"Original" String tool class--get Pinyin (full or initials) corresponding to Chinese characters

Source: Internet
Author: User

1. Introduction of Pinyin4j-2.5.0.jar Package 2. Code implementation:

ImportJava.util.regex.Matcher;ImportJava.util.regex.Pattern;ImportNet.sourceforge.pinyin4j.PinyinHelper;ImportNet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;ImportNet.sourceforge.pinyin4j.format.HanyuPinyinToneType; /*** * Kanji Tool class *@authorCsharper *@since2014.12.26 **/ Public classChinesecharacterutil {/*** * Convert kanji to pinyin (first letter or full spell) *@paramHanzi *@paramFull Spell *@return     */     Public StaticString Converthanzi2pinyin (String Hanzi,BooleanFull ) {        /*** * ^[\u2e80-\u9fff]+$ matches All Olympics languages * ^[\u4e00-\u9fff]+$ matches simplified and traditional * ^[ \u4e00-\u9fa5]+$ Matching simplified*/String RegExp= "^[\u4e00-\u9fff]+$"; StringBuffer SB=NewStringBuffer (); if(hanzi==NULL||"". Equals (Hanzi.trim ())) {            return""; } String Pinyin="";  for(intI=0;i)        {            Charunit=Hanzi.charat (i); if(Match (string.valueof (unit), REGEXP))//is kanji, then pinyin{Pinyin=Convertsinglehanzi2pinyin (unit); if(Full) {sb.append (pinyin); }                Else{sb.append (Pinyin.charat (0)); }            }            Else{sb.append (unit); }        }        returnsb.tostring (); }    /*** * Convert individual characters to pinyin *@paramHanzi *@return     */    Private StaticString Convertsinglehanzi2pinyin (CharHanzi) {Hanyupinyinoutputformat OutputFormat=NewHanyupinyinoutputformat ();        Outputformat.settonetype (Hanyupinyintonetype.without_tone);        String[] Res; StringBuffer SB=NewStringBuffer (); Try{res=Pinyinhelper.tohanyupinyinstringarray (Hanzi,outputformat); Sb.append (res[0]);//for Polyphone, use only the first pinyin}Catch(Exception e) {e.printstacktrace (); return""; }        returnsb.tostring (); }        /***     * @paramSTR source String *@paramregex Regular Expression *@returnis matched*/     Public Static Booleanmatch (String str,string regex) {pattern pattern=pattern.compile (regex); Matcher Matcher=Pattern.matcher (str); returnMatcher.find (); }     Public Static voidMain (string[] args) {System.out.println (Converthanzi2pinyin ("I am a Chinese 123abc",true)); }}

3. Operation Result: (1) Full spell: WOSHIZHONGGUOREN123ABC (2) First letter: WSZGR123ABC

"Original" String tool class--get Pinyin (full or initials) corresponding to Chinese characters

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.