Java Kanji Pinyin Class. Using PINYIN4J to implement
Package com.pinyin4j;
Import Net.sourceforge.pinyin4j.PinyinHelper;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
Import Net.sourceforge.pinyin4j.format.HanyuPinyinToneType; /** * Chinese Pinyin tools <br> * pinyin4j * * * @author Jayden * * * * */public class Pinyinutil {private static Pinyinut
Il instance;
public static Pinyinutil getinstance () {if (instance = null) {instance = new Pinyinutil ();
return instance;
Private Hanyupinyinoutputformat OutputFormat = null; Private Hanyupinyinoutputformat GetOutputFormat () {if (OutputFormat = null) {OutputFormat = new Hanyupinyinoutputf
Ormat ();
Outputformat.settonetype (Hanyupinyintonetype.without_tone);
Outputformat.setcasetype (hanyupinyincasetype.lowercase);
return OutputFormat; /** * Gets all characters in the string * * @param str * @return * @throws Exception/public string getstringinitial (Strin G str) {StringBuffer SBF = new StringBuffer ();
if (str!= null) {for (int i = 0; i < str.length (); i++) {Sbf.append getcharinitial (Str.charat (i)));
} return sbf.tostring (); /** * Get Chinese character First letter * * @param c * @return * @throws Exception/public String getcharinitial (char c) {T
ry {string[] STRs = Pinyinhelper.tohanyupinyinstringarray (c, GetOutputFormat ());
String initial = "";
if (STRs!= null && strs.length > 0) {String str = strs[0];
if (str!= null && str.length () > 0) {initial + = Str.charat (0);
} return initial;
catch (Exception ex) {ex.printstacktrace ();
return C + "";
} public string Getstringpinyin (string chines) {char[] Namechar = Chines.tochararray ();
String pinyinstr = "";
for (int i = 0; i < namechar.length; i++) {try {char cha=namechar[i]; if (Namechar[i] > 128) {pinyinstr = Pinyinhelper.tohanyupinyinstringarray (Cha, GetOutputFormat ()) [0];
} catch (Exception ex) {ex.printstacktrace ();
Pinyinstr + = Namechar[i];
} return PINYINSTR; /** * Acquire character Fu Quan spell * * @param c * @return/public String Getcharpinyin (char c) {try {string[] STRs = P
Inyinhelper.tohanyupinyinstringarray (c, GetOutputFormat ());
String str = "";
if (STRs!= null && strs.length > 0) {str = strs[0];
return str;
catch (Exception ex) {ex.printstacktrace ();
return C + ""; } public static void Main (string[] args) {String stringinitial = Pinyinutil.getinstance (). Getstringinitial ("Yue is an uncommon word.
The general phonetic code cannot parse ");
System.out.println (stringinitial);
String Stringpinyin = Pinyinutil.getinstance (). Getstringpinyin ("Yue is an obscure word general phonetic code cannot parse");
System.out.println (Stringpinyin); }
}