Terminator: converts Chinese characters into pinyin characters using the jar package related to pinyin4j

Source: Internet
Author: User

Terminator: converts Chinese characters into pinyin characters using the jar package related to pinyin4j

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 {/*** Convert words into pinyin. * @ Param caseType when the value is 1, the first letter is obtained in lowercase; otherwise, the first letter is in upper case. * @ Author Gao huanjie */public static String getPinYinAllChar (String zn_str, int caseType) {char [] strChar = zn_str.toCharArray (); HanyuPinyinOutputFormat hanYuPinOutputFormat = new callback (); // if (1 = caseType) {hanYuPinOutputFormat. setCaseType (HanyuPinyinCaseType. LOWERCASE);} else {hanYuPinOutputFormat. setCaseType (HanyuPinyinCaseType. UPPERCASE);} hanYuPinOutputFormat. setToneType (HanyuPinyinToneType. WITHOUT_TONE); hanYuPinOutputFormat. setVCharType (HanyuPinyinVCharType. WITH_V); StringBuffer pyStringBuffer = new StringBuffer (); String [] strString = new String [strChar. length]; try {for (int I = 0; I <strChar. length; I ++) {if (Character. toString (strChar [I]). matches ("[\ u4E00-\ u9FA5] +") {// if it is a Chinese character strString = PinyinHelper. toHanyuPinyinStringArray (strChar [I], hanYuPinOutputFormat); // save all the Chinese characters to the pyStringBuffer IN THE strString array. append (strString [0]); // obtain the first pronunciation of the Chinese character and connect it to the string pyStringBuffer} else {// if it is not a Chinese character, directly remove characters and connect to the string pyStringBuffer and then pyStringBuffer. append (Character. toString (strChar [I]) ;}} catch (BadHanyuPinyinOutputFormatCombination e) {e. printStackTrace ();} return pyStringBuffer. toString ();} public static void main (String [] args) {String cnStr = "Liu Feifei"; System. out. println (getPinYinAllChar (cnStr, 1); // output "liufeifei" System. out. println (getPinYinAllChar (cnStr, 2); // output "LIUFEIFEI "}}
[Download related jar packages]

Related Article

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.