Package Com.joyce.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 ;/** * pinyin4j * @author joyce.luo * @date 2015-5-14 pm 01:04:38 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Co Pyright (c) */public class Pinyinutil {public static void main (string[] args) {String str1 = Pinyinutil.topinyinstrin G (' Zhang '); System.out.println ("Chinese char-to" + str1); String str2 = pinyinutil.topinyinstring (' C '); System.out.println ("English char-to" + str2); String STR3 = pinyinutil.topinyinstring ("Zhang San"); System.out.println ("Chinese string----" + STR3); String STR4 = pinyinutil.topinyinstring ("Hello World"); System.out.println ("Chinese string----" + STR4); String STR5 = pinyinutil.topinyinstring ("Hi Zhang San, hello world!"); System.out.println ("ChineSE and 中文版---"+ STR5);} /** * get Pinyin output format * @return Pinyin output format * @author Joyce.luo * @date 2015-5-14 pm 01:40:10 * @version V3.0 * @since Tomcat6. 0,jdk1.6 * @copyright Copyright (c) */private static Hanyupinyinoutputformat Getpinyinformat () { Hanyupinyinoutputformat format = new Hanyupinyinoutputformat ();/* * Uppercase: Uppercase (ZHONG) * Lowercase: lowercase (ZHONG) */format. Setcasetype (hanyupinyincasetype.lowercase); */* Without_tone: No phonetic transcription (zhong) * with_tone_number:1-4 number denotes English standard (ZHONG4) * with_ Tone_mark: Direct with phonetic symbols (must with_u_unicode otherwise abnormal) (Zhòng) */format.settonetype (hanyupinyintonetype.without_tone);/* WITH_ V: v means u (nv) * With_u_and_colon: With "u:" Means U (nu:) * With_u_unicode: Direct with U (nü) */format.setvchartype ( Hanyupinyinvchartype.with_u_unicode);//return the output format to return to format;} /** * Converts a character to pinyin * if it is an English character, the direct output * If the characters are Polyphone, take the first * @param c to convert the character * @return converted String * @author Joyce.luo * @date 2015-5-14 Afternoon 01:34:55 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public static String TopinyiNstring (char c) {Hanyupinyinoutputformat format = Pinyinutil.getpinyinformat (); try {string[] Pinyin = Pinyinhelper.tohanyupinyinstringarray (c, format); if (null = = Pinyin | | Pinyin.length < 1) {return string.valueof (c);} return pinyin[0];} catch (Exception e) {e.printstacktrace (); return null;}} /** * String converted to Pinyin * @param str to be converted String * @return converted String * @author Joyce.luo * @date 2015-5-14 pm 01:38:17 * @version V3.0 * @since tomcat6.0,jdk1.6 * @copyright Copyright (c) */public static String topinyinstring (String str) {if (null = = str || "". Equals (str)) {return null;} StringBuilder sb = new StringBuilder (); String Temppinyin = null;for (int i = 0; i < str.length (); i++) {Temppinyin = pinyinutil.topinyinstring (Str.charat (i)); Sb.append (Temppinyin);} return sb.tostring ();}}
Java Kanji to pinyin pinyin4j