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.HanyuPinyinVCharType ; Import Net.sourceforge.pinyin4j.format.exception.badhanyupinyinoutputformatcombination;public class Test {/** * Extracts the first letter of the Chinese character, if it contains a fee Chinese characters are ignored, if all non-English is returned "". * @param Casetype when the first letter obtained for 1 o'clock is lowercase, otherwise uppercase. * @author Gao Yingjie */public static String Getpinyinheadchar (string zn_str, int casetype) {if (zn_str! = null &&A MP;!zn_str.trim (). Equalsignorecase ("")) {char[] Strchar = Zn_str.tochararray (); Hanyu Pinyin format Output class Hanyupinyinoutputformat Hanyupinoutputformat = new Hanyupinyinoutputformat (); Output settings, case, phonetic notation, etc. if (1 = = Casetype) {Hanyupinoutputformat.setcasetype (hanyupinyincasetype.lower case); } else {HanyupinoutputforMat.setcasetype (hanyupinyincasetype.uppercase); } hanyupinoutputformat.settonetype (Hanyupinyintonetype.without_tone); Hanyupinoutputformat.setvchartype (HANYUPINYINVCHARTYPE.WITH_V); StringBuffer pystringbuffer = new StringBuffer (); for (int i=0; i<strchar.length; i++) {char c = strchar[i]; Char pyc = strchar[i]; if (string.valueof (c). Matches ("[\\u4e00-\\u9fa5]+")) {//is Chinese or a-Z or a-z conversion pinyin try {S tring[] Pystirngarray = Pinyinhelper.tohanyupinyinstringarray (Strchar[i], hanyupinoutputformat); if (null! = Pystirngarray && pystirngarray[0]!=null) {pyc = Pystirngarray[0].charat (0); Pystringbuffer.append (PYC); }} catch (Badhanyupinyinoutputformatcombination e) {e.printstacktrace (); } }} return pystringbuffer.tostring (); } return null; } public static void Main (string[] args) {String cnstr = "Liu Fifi"; System.out.println (Getpinyinheadchar (CNSTR, 1)); Output LffSystem.out.println (Getpinyinheadchar (CNSTR, 2)); Output LFF}}
"0-minute download of related jar packages"
Terminator: Extract the first letter of Chinese characters with pinyin4j related jar package