Parfois, nous rencontrons cette situation, et nous devons obtenir certains pinyin chinois, abréviations chinoises et initiales chinoises. Ci-dessous, je vous présenterai comment obtenir des abréviations chinoises de pinyin.
1. Établissement et configuration du projet
Tout d'abord, nous créons un projet Java, créons un nouveau dossier LIBS et introduisons un 734A7099-4830-39F2-A136-0E850CCDCC7A.jar. Je crois que cette étape n'est pas nécessaire pour être écrite en détail, sautez-la.
2. Obtenir le pinyin chinois (comme: Province du Guangdong -> Guangdongsheng)
</ pre> <pren name = "code"> <span style = "white-space: pre"> </ span> / ** * Obtenez l'orthographe chinoise complète * @param src la chaîne chinoise qui doit être convertie * @return * / public static static getpingyin (String src) {char [] t1 = null; t1 = src.tocharArray (); String [] t2 = new String [t1.length]; HanyupinyInOutputFormat t3 = new HanyupinyInOutputFormat (); t3.setCasetype (hanyupinyincasetype.lowercase); t3.SetToneType (hanyupinyIntOnType.without_tone); t3.setvchartype (hanyupinyinvchartype.with_v); String t4 = ""; int t0 = t1.length; essayez {pour (int i = 0; i <t0; i ++) {// Déterminez s'il s'agit d'un caractère chinois if (java.lang.character.tostring (t1 [i]). correspond ("[// u4e00 - // u9fa5] +")) {t2 = pinyinheper.tohanyupinyInStringArray (t1 [i], T3); t4 + = t2 [0]; } else {t4 + = java.lang.character.toString (t1 [i]); }} return t4; } catch (badhanyupinyInOutPutformatCombination e1) {e1.printStackTrace (); } return t4; } 3. Obtenez l'abréviation des acronymes chinois (tels que: Province du Guangdong -> GDS)
</ pre> <pren name = "code"> <span style = "white-space: pre"> </ span> / ** * Obtenez la lettre initiale chinoise * @param str de la chaîne chinoise qui doit être convertie * @return * / public static static getPinyInheadChar (String str) {String convert = ""; pour (int j = 0; j <str.length (); j ++) {word char = str.charat (j); String [] pinyInArray = PinyInhelper.toHanyUpinyInstringArray (Word); if (pinyInArray! = null) {convert + = pinyInArray [0] .Charat (0); } else {convert + = word; }} return convert; } 4. Obtenez la lettre initiale chinoise et convertissez-la en majuscules (comme: Province du Guangdong -> g)
Nous devons combiner la méthode GetPinyInheadChar de l'étape 3, le code est le suivant:
</ pre> <pren name = "code"> <span style = "white-space: pre"> </ span> string s = getpinyinheadchar ("province de Guangdong"); System.out.println ("Obtenez l'abréviation de Pinyin:" + S); StringBuffer sb = new StringBuffer (S); if (sb.length ()> 1) {String ss = sb.delete (1, sb.length ()). toString (); System.out.println ("Obtenez la lettre initiale:" + caractères.touppercase (ss.tocharArray () [0]) + "");Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.