(简单易懂)取一段汉字首字母

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java 获取一段汉字的拼音首字母

public class sorts {
public static void main(String[] args)
{
System.out.print(ChineseToPYAbbreviation("拼音首字母"));
}

//下面两个方法可以放在一个工具类里面。不用懂原理,直接调用ChineseToPYAbbreviation()。返回的就是一段汉字的每个汉字的首字母

/**
* 获取一段汉字的每个汉字的首字母
* @param str
* @return
*/
public static String ChineseToPYAbbreviation(String str)
{
char [] tmp= str.toCharArray();
String tempStr = "";
for(int i=0;i{
if ((int)tmp[i] >= 33 && (int)tmp[i]<= 126)
{
//字母和符号原样保留
tempStr += tmp[i];
}
else
//累加拼音声母
tempStr +=getAlpha(tmp[i]+"");
}
return tempStr;
}



/**
* 获取拼音首字母
* @param str
* @return
*/
public static String getAlpha(String str)
{
String firstChar = "*";
if ( str == null || str.length () <= 0 ) return firstChar;

try {
byte firstCharBytes[] = new byte[ 2 ];
int gbcode;

firstCharBytes[ 0 ] = str.getBytes ( "gb2312" )[ 0 ];
gbcode = firstCharBytes[ 0 ] & 0x000000ff;
if ( str.length () > 1 || gbcode >= 0xb0 ) {
firstCharBytes[ 1 ] = str.getBytes ( "gb2312" )[ 1 ];
gbcode = ( firstCharBytes[ 0 ] & 0x000000ff ) * 0x100 + ( firstCharBytes[ 1 ] & 0x000000ff );
}

if ( gbcode >= 0xb0a1 && gbcode <= 0xb0c4 )
firstChar = "a";
else if ( gbcode >= 0xb0c5 && gbcode <= 0xb2c0 )
firstChar = "b";
else if ( gbcode >= 0xb2c1 && gbcode <= 0xb4ed )
firstChar = "c";
else if ( gbcode >= 0xb4ee && gbcode <= 0xb6e9 )
firstChar = "d";
else if ( gbcode >= 0xb6ea && gbcode <= 0xb7a1 )
firstChar = "e";
else if ( gbcode >= 0xb7a2 && gbcode <= 0xb8c0 )
firstChar = "f";
else if ( gbcode >= 0xb8c1 && gbcode <= 0xb9fd )
firstChar = "g";
else if ( gbcode >= 0xb9fe && gbcode <= 0xbbf6 )
firstChar = "h";
else if ( gbcode >= 0xbbf7 && gbcode <= 0xbfa5 )
firstChar = "j";
else if ( gbcode >= 0xbfa6 && gbcode <= 0xc0ab )
firstChar = "k";
else if ( gbcode >= 0xc0ac && gbcode <= 0xc2e7 )
firstChar = "l";
else if ( gbcode >= 0xc2e8 && gbcode <= 0xc4c2 )
firstChar = "m";
else if ( gbcode >= 0xc4c3 && gbcode <= 0xc5b5 )
firstChar = "n";
else if ( gbcode >= 0xc5b6 && gbcode <= 0xc5bd )
firstChar = "o";
else if ( gbcode >= 0xc5be && gbcode <= 0xc6d9 )
firstChar = "p";
else if ( gbcode >= 0xc6da && gbcode <= 0xc8ba )
firstChar = "q";
else if ( gbcode >= 0xc8bb && gbcode <= 0xc8f5 )
firstChar = "r";
else if ( gbcode >= 0xc8f6 && gbcode <= 0xcbf9 )
firstChar = "s";
else if ( gbcode >= 0xcbfa && gbcode <=

0xcdd9 )
firstChar = "t";
else if ( gbcode >= 0xcdda && gbcode <= 0xcef3 )
firstChar = "w";
else if ( gbcode >= 0xcef4 && gbcode <= 0xd1b8 )
firstChar = "x";
else if ( gbcode >= 0xd1b9 && gbcode <= 0xd4d0 )
firstChar = "y";
else if ( gbcode >= 0xd4d1 && gbcode <= 0xd7f9 )
firstChar = "z";
else
gbcode = firstCharBytes[ 0 ];

if ( gbcode >= 'A' && gbcode <= 'Z' )
gbcode += 32;
if ( gbcode >= 'a' && gbcode <= 'z' )
firstChar = String.valueOf ( ( char ) gbcode );
}
catch ( Exception e ) {
System.out.println ( "getFirstCharOfString Exception: " + e.getMessage () );
}

return firstChar;
}
}

相关文档
最新文档