Terminator: Extract the first letter of Chinese characters with pinyin4j related jar package

Source: Internet
Author: User

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 &AMP;&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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.