First, what is pinyin4j
Pinyin4j is a popular Java library that supports conversion between Chinese characters and pinyin. Pinyin output format can be customized.
Second, how to use?
Basic use, see my article reproduced in this can be
http://blog.csdn.net/stoppig/article/details/40889413
Iii. Use of extensions
1. Use in Android
Create a new Android project and put the PINYIN4J package into the Libs folder. Then import the appropriate class to use in the class that you want to use.
Import Android.util.log;import Net.sourceforge.pinyin4j.pinyinhelper;import Net.sourceforge.pinyin4j.format.hanyupinyincasetype;import Net.sourceforge.pinyin4j.format.hanyupinyinoutputformat;import Net.sourceforge.pinyin4j.format.hanyupinyintonetype;import Net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
This step allows you to freely use the features in pinyin4j.
2. Use pinyin4j to generate spelling shorthand
When you use PINYIN4J, you first split the Chinese string and split it into characters. After parsing each character and generating a string array of English letters, we only need to take the No. 0-bit string of the parsed string array of each character, and combine it to get a phonetic shorthand for a Chinese string. The following is the sample code.
public static string Convertertofirstspell (String chines) {if (Chines = = null) {LOG.E (TAG, "string value = null, return!"); Return "";} String pinyinname = ""; try {char[] Namechar = Chines.tochararray (); getinstance (). Setcasetype ( Hanyupinyincasetype.uppercase); getinstance (). Settonetype (Hanyupinyintonetype.without_tone); for (int i = 0; i < Namechar.length; i++) {if (Namechar[i] >) {try {pinyinname + pinyinhelper.tohanyupinyinstringarray (Namechar[i], getinstance ()) [0 ].charat (0);} catch (Badhanyupinyinoutputformatcombination e) {log.e (TAG, E + "");}} else {pinyinname + = Namechar[i];}}} catch (Exception e) {log.e (TAG, E + "");p Inyinname = "";} return pinyinname;}
These are some of the tutorials used by pinyin4j in Android, the following is a sample code download link:
Sample code
pinyin4j use in Android