Pinyin4j-2.5.0.jar such a jar package was used;
The specific implementation code is as follows: 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 Hanzitopinyin {/** * converts Chinese characters to full spell * * @param src * @return String */public static string Getpinyin (String src)
{char[] T1 = null; t1 = Src.tochararray ();//System.out.println (t1.length);
string[] t2 = new String[t1.length];
System.out.println (t2.length);
Set the format of Chinese pinyin output 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++) {//To determine if it is a kanji character
System.out.println (T1[i]); if (character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) {t2 = Pinyinhelper.tohanyupinyinstringarray (t1[i], T3) //A few of the Chinese characters are all spelled in the T2 array T4 + + t2[0];//take out the first pronunciation of the Chinese spelling and connect to the string t4 after} else {//If not kanji characters, directly remove the character and connect to the string t4 after T4 + = character.tostring
(T1[i]); catch (Badhanyupinyinoutputformatcombination e) {//TODO auto-generated catch block E.printstacktrace ();
4.toUpperCase (); /** * Extracts the first letter of each kanji * * @param str * @return string/public static string Getpinyinheadchar (String str) {string conve
RT = ""; for (int j = 0; J < Str.length (); j + +) {char word = Str.charat (j);//extract the first letter of Chinese characters string[] Pinyinarray = Pinyinhelper.toh
Anyupinyinstringarray (word);
if (Pinyinarray!= null) {convert + = Pinyinarray[0].charat (0);} else {convert + = Word;}}
return Convert.touppercase (); /** * Converts string to ASCII * * @param cnstr * @return string/public static string Getcnascii (String cnstr) {StringBuffer
Strbuf = new StringBuffer (); Convert string to byte sequence byte[] BGBK = Cnstr.getbytes (); for (int i = 0; i < bgbk.length i++) {//System.out.println (Integer.tohexstring (bgbk[i) & 0xff);//convert each character to ASci
I code strbuf.append (integer.tohexstring (bgbk[i) & 0xff));
return strbuf.tostring (); }
}