java中把汉字转换成简拼的实现代码
程序员文章站
2023-12-17 14:04:28
复制代码 代码如下:public static void main(string[] args) { string str = null; str...
复制代码 代码如下:
public static void main(string[] args) {
string str = null;
str = "顾亮";
system.out.println("spell=" + tojp(str));
}
public static string tojp(string c){
char[] chars = c.tochararray();
stringbuffer sb = new stringbuffer("");
for(int i=0;i<chars.length;i++){
sb.append(getjp(chars[i]));
}
return sb.tostring().touppercase();
}
public static string getjp(char c)
{
byte[] array = new byte[2];
try {
array = string.valueof(c).getbytes("gbk");
} catch (unsupportedencodingexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
if(array.length<2)return string.valueof(c);
int i = (short)(array[0] - '/0' + 256) * 256 + ((short)(array[1] - '/0' + 256));
if ( i < 0xb0a1) return string.valueof(c);
if ( i < 0xb0c5) return "a";
if ( i < 0xb2c1) return "b";
if ( i < 0xb4ee) return "c";
if ( i < 0xb6ea) return "d";
if ( i < 0xb7a2) return "e";
if ( i < 0xb8c1) return "f";
if ( i < 0xb9fe) return "g";
if ( i < 0xbbf7) return "h";
if ( i < 0xbfa6) return "j";
if ( i < 0xc0ac) return "k";
if ( i < 0xc2e8) return "l";
if ( i < 0xc4c3) return "m";
if ( i < 0xc5b6) return "n";
if ( i < 0xc5be) return "o";
if ( i < 0xc6da) return "p";
if ( i < 0xc8bb) return "q";
if ( i < 0xc8f6) return "r";
if ( i < 0xcbfa) return "s";
if ( i < 0xcdda) return "t";
if ( i < 0xcef4) return "w";
if ( i < 0xd1b9) return "x";
if ( i < 0xd4d1) return "y";
if ( i < 0xd7fa) return "z";
return string.valueof(c);
}