Contoh -contoh dalam artikel ini berbagi kode spesifik karakter Java China ke pinyin tool untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
impor com.google.common.base.strings; impor net.sourceForge.pinyin4j.pinyinhelper; impor net.sourceForge.pinyin4j.format.hanyupinyinOutputFormat; impor net.sourceForge.pinyin4j.form.format.hanyupinypformat; net.sourceForge.pinyin4j.format.exception.badhanyupinyinOutputFormatcombination; impor org.slf4j.logger; impor org.slf4j.loggerfactory; impor java.util.arraylist; impor java.util.list; impor javaet; Pinyinutils {private static final Logger Logger = loggerFactory.getLogger (pinyinutils.class); / ** * Parsing kata tunggal * * @param str pertama * @return */ public static string [] convert (string str) {string [] reslut = null; HanyupinyinOutputFormat HanyupinyinOutputFormat = HanyupinyinOutPutFormat baru (); hanyupinyinoutputFormat.settonetype (hanyupinyintonetype.without_tone); coba {reslut = pinyinhelper.tohanyupinyInstringarray (str.charat (0), hanyupinyinOutputFormat); TreeSet <string> stringTreeSet = TreeSet baru <> (); untuk (int i = 0; i <reslut.length; i ++) {if (reslut.length> = 3) {break; } stringtreeset.add (reslut [i] .replace ("u:", "v")); } reslut = string baru [stringtreeset.size ()]; reslut = stringtreeset.toArray (reslut); } catch (badhanyupinyinOutputFormatcombination badhanyupinyinoutputFormatcombination) {badhanyupinyinOutputFormatcombination.printstacktrace (); } catch (Exception e) {Logger.Error ("[Convert]:", e); } return reslut; } / ** * Parsing frasa (penulisan lengkap) * * @param chs * @return * / string statis public getselling (string chs) {return translate (chs, false); } / ** * Karakter Cina ke pinyin * * @param chs * @param akronim * @return * / private static string translate (string chs, boolean singkatan) {stringBuffer buffer = stringBuffer baru (); if (strings.isnullorempty (CHS)) return ""; coba {list <list <string>> temps = new arraylist <> (); int len = chs.length (); int len1 = 0; untuk (int i = 0; i <len; i ++) {Daftar <string> stringList = ArrayList baru <> (); Tombol string = chs.charat (i) + ""; if (key.getBytes (). length> = 2) {string [] temp = convert (key); if (temp.length == 0) {lanjutkan; } if (temp == null) {stringlist.add (""); } else {for (string v: temp) {stringlist.add (v); }}} else {StringList.Add (key); } temps.add (stringlist); len1 ++; } Daftar <Daftar <String>> t = ArrayList baru <> (); untuk (int i = 0; i <len1; i ++) {Daftar <string> currentList = new ArrayList <> (); Daftar <String> stringList = temps.get (i); if (stringlist! = null) {for (string s: stringlist) {if (singkatan) {s = s.charat (0) + ""; } if (i> 0) {Daftar <String> prelist = t.get (i - 1); if (prelist! = null) {for (string s1: prelist) {currentList.add (s1 + s); }}}} else {currentList.add (s); }}} t.add (i, currentList); } if (t.size ()> 0) {Daftar <string> currentList = t.get (t.size ()-1); if (currentList! = null) {for (string current: currentList) {buffer.append (current); buffer.append (""); }}} return buffer.toString (); } catch (exception e) {logger.error ("[getSortletters]:", e); kembali ""; }} / ** * Parsing frase (singkatan) * * @param chs * @return * / string statis public getsmallselling (string chs) {return translate (chs, true); } / ** * Dapatkan huruf awal * * @return * / public static String getSortletters (string pingyin) {coba {string sortstring = pingyin.substring (0, 1) .touppercase (locale.getDefault ()); // Ekspresi reguler untuk menentukan apakah huruf pertama adalah huruf bahasa Inggris IF (sortString.matches ("[az]")) {return sortstring.touppercase (locale.getDefault ()); }} catch (Exception e) {Logger.Error ("[getSortletters]:", e); } kembali "#"; } public static void main (string [] args) {pinyinutils p = new pinyinutils (); System.out.println (p.getselling ("single")); System.out.println (p.getsmallselling ("test")); }} Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.