Convert Kanji to Pinyin tool class:
1 ImportNet.sourceforge.pinyin4j.PinyinHelper;2 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;3 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;4 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinToneType;5 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;6 Importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;7 8 Public classPinyinutils {9 /**Ten * Convert Chinese characters to full spell One * A * @paramsrc - * @returnString - */ the Public Staticstring Getpinyin (string src) { - Char[] T1 =NULL; -T1 =Src.tochararray (); -string[] T2 =NewString[t1.length]; + //format Chinese Pinyin output -hanyupinyinoutputformat t3 =NewHanyupinyinoutputformat (); + T3.setcasetype (hanyupinyincasetype.lowercase); A T3.settonetype (hanyupinyintonetype.without_tone); at T3.setvchartype (hanyupinyinvchartype.with_v); -String T4 = ""; - intT0 =t1.length; - Try { - for(inti = 0; i < t0; i++) { - //determine if the character is Chinese characters in if(Character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) { -T2 = Pinyinhelper.tohanyupinyinstringarray (t1[i], T3);//Save all the Chinese characters in the T2 array toT4 + = t2[0];//Take out the first pronunciation of the Chinese character and connect to the string T4 +}Else { - //if it is not a kanji character, remove the character directly and connect to the string T4 theT4 + =character.tostring (T1[i]); * } $ }Panax Notoginseng}Catch(badhanyupinyinoutputformatcombination e) { - //TODO auto-generated Catch block the e.printstacktrace (); + } A returnT4; the } + - /** $ * Extract the first letter of each Chinese character $ * - * @paramStr - * @returnString the */ - Public Staticstring Getpinyinheadchar (String str) {WuyiString convert = ""; the for(intj = 0; J < Str.length (); J + +) { - CharWord =Str.charat (j); Wu //extract the first letter of Chinese characters -string[] Pinyinarray =Pinyinhelper.tohanyupinyinstringarray (word); About if(Pinyinarray! =NULL) { $convert + = Pinyinarray[0].charat (0); -}Else { -Convert + =Word; - } A } + returnconvert; the } - $ /** the * Convert string to ASCII code the * the * @paramcnstr the * @returnString - */ in Public Staticstring Getcnascii (String cnstr) { theStringBuffer strbuf =NewStringBuffer (); the //Convert a string into a sequence of bytes About byte[] BGBK =cnstr.getbytes (); the for(inti = 0; i < bgbk.length; i++) { the //convert each character to an ASCII code theStrbuf.append (Integer.tohexstring (Bgbk[i] & 0xff) + ""); + } - returnstrbuf.tostring (); the }Bayi the}
Java converts Chinese characters into pinyin