java-Kanji Conversion Pinyin-pinyin4j.jar

Source: Internet
Author: User

Use pinyin4j to convert kanji to Pinyin, pinyin4j jar package with attachments

ImportNet.sourceforge.pinyin4j.PinyinHelper;ImportNet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;ImportNet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;ImportNet.sourceforge.pinyin4j.format.HanyuPinyinToneType;ImportNet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;Importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; Public classChinesetoenglish {//convert Chinese characters to full spell     Public Staticstring Getpingyin (string src) {Char[] T1 =NULL; T1=Src.tochararray (); string[] T2=NewString[t1.length]; Hanyupinyinoutputformat T3=NewHanyupinyinoutputformat ();        T3.setcasetype (hanyupinyincasetype.lowercase);        T3.settonetype (Hanyupinyintonetype.without_tone);        T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V); String T4= ""; intT0 =t1.length; Try {             for(inti = 0; i < t0; i++) {                //determine if the character is Chinese characters                if(Java.lang.Character.toString (T1[i]). Matches ("[\\u4e00-\\u9fa5]+]) ) {T2=Pinyinhelper.tohanyupinyinstringarray (t1[i], T3); T4+ = T2[0]; } ElseT4+=java.lang.Character.toString (T1[i]); }            //System.out.println (T4);            returnT4; } Catch(badhanyupinyinoutputformatcombination E1) {e1.printstacktrace (); }        returnT4; }    //returns the first letter of Chinese     Public Staticstring Getpinyinheadchar (String str) {string convert= "";  for(intj = 0; J < Str.length (); J + +) {            CharWord =Str.charat (j); String[] Pinyinarray=Pinyinhelper.tohanyupinyinstringarray (word); if(Pinyinarray! =NULL) {convert+ = Pinyinarray[0].charat (0); } Else{convert+=Word; }        }        returnconvert; }    //To transfer a string to an ASCII code     Public Staticstring Getcnascii (String cnstr) {StringBuffer strbuf=NewStringBuffer (); byte[] BGBK =cnstr.getbytes ();  for(inti = 0; i < bgbk.length; i++) {strbuf.append (integer.tohexstring (bgbk[i)& 0xFF)); }        returnstrbuf.tostring (); }     Public Static voidMain (string[] args) {System.out.println (Getpingyin ("Qijiang QQ County")); System.out.println (Getpinyinheadchar ("Qijiang County")); System.out.println (Getcnascii ("Qijiang County")); }}

Reproduced in this article

Jar

Maven Configuration Download

        <Dependency>            <groupId>Com.belerweb</groupId>            <Artifactid>Pinyin4j</Artifactid>            <version>2.5.0</version>        </Dependency>

java-Kanji Conversion Pinyin-pinyin4j.jar

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.