Java converts Chinese characters into pinyin

Source: Internet
Author: User

Convert Kanji to Pinyin tool class:

1 ImportNet.sourceforge.pinyin4j.PinyinHelper;2 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;3 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;4 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinToneType;5 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;6 Importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;7 8  Public classPinyinutils {9     /**Ten * Convert Chinese characters to full spell One      *  A      * @paramsrc -      * @returnString -      */ the      Public Staticstring Getpinyin (string src) { -         Char[] T1 =NULL; -T1 =Src.tochararray (); -string[] T2 =NewString[t1.length]; +         //format Chinese Pinyin output -hanyupinyinoutputformat t3 =NewHanyupinyinoutputformat (); + T3.setcasetype (hanyupinyincasetype.lowercase); A T3.settonetype (hanyupinyintonetype.without_tone); at T3.setvchartype (hanyupinyinvchartype.with_v); -String T4 = ""; -         intT0 =t1.length; -         Try { -              for(inti = 0; i < t0; i++) { -                 //determine if the character is Chinese characters in                 if(Character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) { -T2 = Pinyinhelper.tohanyupinyinstringarray (t1[i], T3);//Save all the Chinese characters in the T2 array toT4 + = t2[0];//Take out the first pronunciation of the Chinese character and connect to the string T4 +}Else { -                     //if it is not a kanji character, remove the character directly and connect to the string T4 theT4 + =character.tostring (T1[i]); *                 } $             }Panax Notoginseng}Catch(badhanyupinyinoutputformatcombination e) { -             //TODO auto-generated Catch block the e.printstacktrace (); +         } A         returnT4; the     } +  -     /** $ * Extract the first letter of each Chinese character $      *  -      * @paramStr -      * @returnString the      */ -      Public Staticstring Getpinyinheadchar (String str) {WuyiString convert = ""; the          for(intj = 0; J < Str.length (); J + +) { -             CharWord =Str.charat (j); Wu             //extract the first letter of Chinese characters -string[] Pinyinarray =Pinyinhelper.tohanyupinyinstringarray (word); About             if(Pinyinarray! =NULL) { $convert + = Pinyinarray[0].charat (0); -}Else { -Convert + =Word; -             } A         } +         returnconvert; the     } -  $     /** the * Convert string to ASCII code the      *  the      * @paramcnstr the      * @returnString -      */ in      Public Staticstring Getcnascii (String cnstr) { theStringBuffer strbuf =NewStringBuffer (); the         //Convert a string into a sequence of bytes About         byte[] BGBK =cnstr.getbytes (); the          for(inti = 0; i < bgbk.length; i++) { the             //convert each character to an ASCII code theStrbuf.append (Integer.tohexstring (Bgbk[i] & 0xff) + ""); +         } -         returnstrbuf.tostring (); the     }Bayi  the}

Java converts Chinese characters into pinyin

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.