Terminator: converts Chinese characters into pinyin characters using the jar package related to pinyin4j
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 Test {/*** Convert words into pinyin. * @ Param caseType when the value is 1, the first letter is obtained in lowercase; otherwise, the first letter is in upper case. * @ Author Gao huanjie */public static String getPinYinAllChar (String zn_str, int caseType) {char [] strChar = zn_str.toCharArray (); HanyuPinyinOutputFormat hanYuPinOutputFormat = new callback (); // if (1 = caseType) {hanYuPinOutputFormat. setCaseType (HanyuPinyinCaseType. LOWERCASE);} else {hanYuPinOutputFormat. setCaseType (HanyuPinyinCaseType. UPPERCASE);} hanYuPinOutputFormat. setToneType (HanyuPinyinToneType. WITHOUT_TONE); hanYuPinOutputFormat. setVCharType (HanyuPinyinVCharType. WITH_V); StringBuffer pyStringBuffer = new StringBuffer (); String [] strString = new String [strChar. length]; try {for (int I = 0; I <strChar. length; I ++) {if (Character. toString (strChar [I]). matches ("[\ u4E00-\ u9FA5] +") {// if it is a Chinese character strString = PinyinHelper. toHanyuPinyinStringArray (strChar [I], hanYuPinOutputFormat); // save all the Chinese characters to the pyStringBuffer IN THE strString array. append (strString [0]); // obtain the first pronunciation of the Chinese character and connect it to the string pyStringBuffer} else {// if it is not a Chinese character, directly remove characters and connect to the string pyStringBuffer and then pyStringBuffer. append (Character. toString (strChar [I]) ;}} catch (BadHanyuPinyinOutputFormatCombination e) {e. printStackTrace ();} return pyStringBuffer. toString ();} public static void main (String [] args) {String cnStr = "Liu Feifei"; System. out. println (getPinYinAllChar (cnStr, 1); // output "liufeifei" System. out. println (getPinYinAllChar (cnStr, 2); // output "LIUFEIFEI "}}
[Download related jar packages]