Sometimes in order to facilitate the development of operating procedures, the need to convert Chinese characters to pinyin and other operations. The following is a combination of their own online information, plus their own in the company's project in-person practice. The complete implementation of the conversion of Chinese characters to pinyin operation. This demo is only responsible for converting it, testing it in the main method, and just calling the methods in this class in the actual need. I am also in the study summary, welcome you big God to make brick to teach, I email: [Email protected]. Reprint this blog, please in the article obvious location marked the source of the article (itred's blog: http://www.cnblogs.com/itred).
First, the test results are posted:
Test parameters:
Convert Kanji to Pinyin
Convert Kanji to Pinyin
the code for the main test method:
1 Public Static void Main (string[] args) {2 // Convert to first letter capitalization 3 System.out.println (Topinyin ("Kanji conversion to Pinyin"4 }
This feature is implemented using the Java Open Source Library, which requires a jar package to develop this program. I use the Pinyin4j-2.5.0.jar. It can be downloaded directly online or on its website. In this ominous statement. If you are not happy, you can download this jar package by clicking Download.
Put out the source code to achieve the demo:
1 Packagecom.red.test;2 3 ImportNet.sourceforge.pinyin4j.PinyinHelper;4 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;5 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;6 ImportNet.sourceforge.pinyin4j.format.HanyuPinyinToneType;7 Importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;8 9 /**Ten * Kanji converted to pinyin One * @authorRed A */ - Public classPinyindemo { - /** the * Test Main method - * @paramargs - */ - Public Static voidMain (string[] args) { +System.out.println (Tofirstchar ("Kanji conversion to Pinyin"). toUpperCase ());//Convert to first letter capitalization -System.out.println (Topinyin ("Convert kanji to Pinyin"))); + } A /** at * Get the first letter of the string pinyin - * @paramChinese - * @return - */ - Public Staticstring Tofirstchar (string Chinese) { -String pinyinstr = ""; in Char[] Newchar = Chinese.tochararray ();//convert to a single character -Hanyupinyinoutputformat DefaultFormat =NewHanyupinyinoutputformat (); to Defaultformat.setcasetype (hanyupinyincasetype.lowercase); + Defaultformat.settonetype (Hanyupinyintonetype.without_tone); - for(inti = 0; i < newchar.length; i++) { the if(Newchar[i] > 128) { * Try { $Pinyinstr + = Pinyinhelper.tohanyupinyinstringarray (Newchar[i], DefaultFormat) [0].charat (0); Panax Notoginseng}Catch(badhanyupinyinoutputformatcombination e) { - E.printstacktrace (); the } +}Else{ APinyinstr + =Newchar[i]; the } + } - returnPinyinstr; $ } $ - /** - * Kanji conversion to pinyin the * @paramChinese - * @returnWuyi */ the Public Staticstring Topinyin (string Chinese) { -String pinyinstr = ""; Wu Char[] Newchar =Chinese.tochararray (); -Hanyupinyinoutputformat DefaultFormat =NewHanyupinyinoutputformat (); About Defaultformat.setcasetype (hanyupinyincasetype.lowercase); $ Defaultformat.settonetype (Hanyupinyintonetype.without_tone); - for(inti = 0; i < newchar.length; i++) { - if(Newchar[i] > 128) { - Try { APinyinstr + = Pinyinhelper.tohanyupinyinstringarray (Newchar[i], defaultformat) [0]; +}Catch(badhanyupinyinoutputformatcombination e) { the E.printstacktrace (); - } $}Else{ thePinyinstr + =Newchar[i]; the } the } the returnPinyinstr; - } in}
itred Source:http://itred.cnblogs.com Copyright notice: This article is copyright to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Java implementation converts kanji to pinyin