The open-source package is used in the program. You can convert Chinese to full spelling or abbreviations. The Code is as follows :/**
* Author: qiaolei
* Create
*/
Package com. Sure;
Import net. sourceforge. pinyin4j. PinyinHelper;
Import net. sourceforge. pinyin4j. format. HanyuPinyinCaseType;
Import net. sourceforge. pinyin4j. format. HanyuPinyinOutputFormat;
Import net. sourceforge. pinyin4j. format. HanyuPinyinToneType;
Import net. sourceforge. pinyin4j. format. HanyuPinyinVCharType;
Import net. sourceforge. pinyin4j. format. exception. BadHanyuPinyinOutputFormatCombination;
Public class ChineseToEnglish {
// Convert Chinese characters to full spelling
Public static String getPingYin (String src ){
Char [] t1 = null;
T1 = src. toCharArray ();
String [] t2 = new String [t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat ();
T3.setCaseType (HanyuPinyinCaseType. LOWERCASE );
T3.setToneType (HanyuPinyinToneType. WITHOUT_TONE );
T3.setVCharType (HanyuPinyinVCharType. WITH_V );
String t4 = "";
Int t0 = t1.length;
Try {
For (int I = 0; I <t0; I ++)
{
// Determine whether it is a Chinese character
If (java. lang. Character. toString (t1 [I]). matches ("[\ u4E00-\ u9FA5] + "))
{
T2 = PinyinHelper. toHanyuPinyinStringArray (t1 [I], t3 );
T4 + = t2 [0];
}
Else
T4 + = java. lang. Character. toString (t1 [I]);
}
// System. out. println (t4 );
Return t4;
}
Catch (BadHanyuPinyinOutputFormatCombination e1 ){
E1.printStackTrace ();
}
Return t4;
}
// Returns the first letter of a Chinese character.
Public static String getPinYinHeadChar (String str ){
String convert = "";
For (int j = 0; j <str. length (); j ++ ){
Char word = str. charAt (j );
String [] pinyinArray = PinyinHelper. toHanyuPinyinStringArray (word );
If (pinyinArray! = Null ){
Convert + = pinyinArray [0]. charAt (0 );
} Else {
Convert + = word;
}
}
Return convert;
}
// Transfer the string to an ASCII code
Public static String getCnASCII (String cnStr)
{
StringBuffer strBuf = new StringBuffer ();
Byte [] bGBK = cnStr. getBytes ();
For (int I = 0; I <bGBK. length; I ++ ){
// System. out. println (Integer. toHexString (bGBK [I] & 0xff ));
StrBuf. append (Integer. toHexString (bGBK [I] & 0xff ));
}
Return strBuf. toString ();
} Public static void main (String [] args ){
System. out. println (getPingYin ("Qijiang county "));
System. out. println (getPinYinHeadChar ("Qijiang county "));
System. out. println (getCnASCII ("Qijiang county "));
} The result is as follows: qijiangxian
Qjx
F4ebbdadcfd8
This article from "Qiao Lei's blog learning progress" blog, please be sure to keep this source http://sucre.blog.51cto.com/1084905/353603