Los ejemplos en este artículo comparten el código específico de los caracteres chinos de Java a la herramienta Pinyin para su referencia. El contenido específico es el siguiente
import com.google.common.base.strings; import net.sourceforge.pinyin4j.pinyinhelper; import net.sourceforge.pinyin4j.format.hanyupinyinoutputformat; import net.sourceforge.pinyin4j.format.hanyupinyintonetype; importar; importación; net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputFormatCombination; import org.slf4j.logger; import org.slf4j.loggerFactory; import java.util.arrayList; import java.util.list; import java.util.locale; import java.util.util.util. Pinyinutils {private static final logger = loggerFactory.getLogger (pinyinutils.class); / ** * Parsing de una sola palabra * * @param str primero * @return */ public static string [] convert (string str) {string [] reslut = null; Hanyupinyinoutputformat hanyupinyinoutputformat = new HanyupinyinoutputFormat (); hanyupinyinoutputformat.settonetype (hanyupinyiNiTonetype.without_tone); Pruebe {reslut = pinyinhelper.tohanyupinyinstringarray (str.charat (0), hanyupinyinoutputformat); TreeSet <String> stringTreeSet = new TreeSet <> (); for (int i = 0; i <reslut.length; i ++) {if (reslut.length> = 3) {break; } stringTreeSet.Add (reslut [i] .replace ("U:", "V")); } reslut = new String [StringTreeSet.Size ()]; reslut = stringTreeSet.toarray (reslut); } Catch (BadhanyupinyinoutputFormatCombination BadhanyupinyinoutputFormatCombination) {BadhanyupinyinoututputFormatCombination.printStackTrace (); } catch (excepción e) {logger.error ("[convert]:", e); } return reslut; } / ** * Frase analizado (escritura completa) * * @param chs * @return * / public static string getSelling (String chs) {return traduce (chs, false); } / ** * caracteres chinos a pinyin * * @param chs * @param acrónimo * @return * / private static string traduce (string chs, boolean aconym) {stringbuffer buffer = new StringBuffer (); if (strings.isnullorempty (chs)) return ""; Pruebe {List <List <String>> temps = new ArrayList <> (); int len = chs.length (); int len1 = 0; for (int i = 0; i <len; i ++) {list <string> stringList = new ArrayList <> (); Clave de cadena = chs.charat (i) + ""; if (key.getBytes (). longitud> = 2) {string [] temp = convert (key); if (temp.length == 0) {continuar; } if (temp == null) {stringList.add (""); } else {for (string v: temp) {stringList.add (v); }}} else {stringList.Add (clave); } temps.Add (StringList); Len1 ++; } List <List <String>> t = new ArrayList <> (); for (int i = 0; i <len1; i ++) {list <string> currentList = new ArrayList <> (); Lista <String> StringList = temps.get (i); if (stringList! = null) {for (string s: stringList) {if (acronym) {s = s.charat (0) + ""; } if (i> 0) {list <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) {list <String> currentList = t.get (t.size ()-1); if (currentList! = null) {for (string current: currentList) {buffer.append (actual); buffer.append (""); }}} return buffer.ToString (); } catch (excepción e) {logger.error ("[getSortletters]:", e); devolver ""; }} / ** * Frase Parsing (abreviatura) * * @param chs * @return * / public static string getSmallSelling (cadena chs) {return traduce (chs, true); } / ** * Obtenga la letra inicial * * @return * / public static string getSortletters (string pingyin) {try {string sortString = pingyin.substring (0, 1) .ToupperCase (locale.getDefault ()); // expresión regular para determinar si la primera letra es una letra en inglés if (sortString.matches ("[AZ]")) {return sortString.ToUpperCase (locale.getDefault ()); }} catch (excepción e) {logger.error ("[getSortletters]:", e); } devolver "#"; } public static void main (string [] args) {pinyinutils p = new PinyInUtils (); System.out.println (p.getSelling ("Single")); System.out.println (p.getSmallSelling ("Test")); }} Lo anterior es todo el contenido de este artículo. Espero que sea útil para el aprendizaje de todos y espero que todos apoyen más a Wulin.com.