La copie de code est la suivante:
importer net.sourceforge.pinyin4j.pinyinhelper;
importer net.sourceforge.pinyin4j.format.hanyupinyincasEType;
importer net.sourceforge.pinyin4j.format.hanyupinyinoutputformat;
importer net.sourceforge.pinyin4j.format.hanyupinyIntOnType;
importer net.sourceforge.pinyin4j.format.hanyupinyinvchartype;
importer net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatCombination;
classe publique pinyin4jutil {
/ **
* Convertir les caractères chinois en orthographe complète
*
* @param src
* String @return
* /
String statique public getpinyin (String Src) {
char [] t1 = null;
t1 = src.tocharArray ();
// System.out.println (t1.length);
String [] t2 = new String [t1.length];
// System.out.println (t2.Length);
// Définit le format de la sortie de pinyin de caractères chinois
HanyupinyInOutputFormat t3 = new HanyupinyInOutputFormat ();
t3.setCasetype (hanyupinyincasetype.lowercase);
t3.SetToneType (hanyupinyIntOnType.without_tone);
t3.setvchartype (hanyupinyinvchartype.with_v);
String t4 = "";
int t0 = t1.length;
essayer {
pour (int i = 0; i <t0; i ++) {
// déterminer s'il peut s'agir de caractères chinois
// System.out.println (t1 [i]);
if (caractères.toString (t1 [i]). correspond ("[// u4e00 - // u9fa5] +")) {
t2 = pinyinhelper.tohanyupinyinstringarray (t1 [i], t3); // enregistrer toutes les orthographes des caractères chinois dans le tableau T2
t4 + = t2 [0]; // Après avoir retiré la première prononciation du caractère chinois et le connecter à la chaîne T4
} autre {
// Si ce n'est pas un caractère chinois, alors après avoir extrait indirectement le caractère et le connecter à la chaîne T4
t4 + = caractères.tostring (t1 [i]);
}
}
} catch (badhanyupinyinoutputformatcombination e) {
e.printStackTrace ();
}
retour t4;
}
/ **
* Extraire la première lettre de chaque caractère chinois
*
* @param str
* String @return
* /
String statique public getPinyInheadChar (String Str) {
String convert = "";
pour (int j = 0; j <str.length (); j ++) {
Char Word = str.charat (j);
// Extraire la première lettre de caractères chinois
String [] pinyInArray = PinyInhelper.toHanyupinyInstringArray (Word);
if (pinyinarray! = null) {
convert + = pinyInArray [0] .Charat (0);
} autre {
converti + = word;
}
}
retour converti;
}
/ **
* Convertir les chaînes en code ASCII
*
* @param cnstr
* String @return
* /
chaîne statique publique getCnascii (chaîne cnstr) {
StringBuffer strbuf = new StringBuffer ();
// Convertir les chaînes en séquences d'octets
octet [] bgbk = cnstr.getBytes ();
pour (int i = 0; i <bgbk.length; i ++) {
// System.out.println (Integer.tohexString (bgbk [i] & 0xff));
// convertir chaque caractère en code ASCII
strbuf.append (Integer.tohexString (bgbk [i] & 0xff));
}
return strbuf.toString ();
}
public static void main (String [] args) {
Chaîne cnstr = "Chine";
System.out.println (getPinyin (CNSTR));
System.out.println (GetPinyInheadChar (CNSTR));
System.out.println (getCNASCII (CNSTR));
}
}