Converting Chinese characters to PinYin

Source: Internet
Author: User

 

Use pinyin4j. jar.

 

Package COM. wangzhu. pinyin; 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; P Ublic class pinyin {public static void main (string [] ARGs) {pinyin = new pinyin (); system. Out. println (pinyin. getpinyin ("OK! Haha! Welcome to. "); system. Out. println (pinyin. getpinyinheadchar (" Okay! Haha! Welcome. ");}/*** return Chinese pinyin ** @ Param Hanzi * Chinese * @ return Chinese pinyin */private string getpinyin (string Hanzi) {stringbuffer pinyin = new stringbuffer (); char [] hanzichar = Hanzi. tochararray (); hanyupinyinoutputformat pinyinoutputformat = new hanyupinyinoutputformat (); pinyinoutputformat. setcasetype (hanyupinyincasetype. lowercase); pinyinoutputformat. settonetype (hanyupinyintonetype. without_tone); P Inyinoutputformat. setvchartype (hanyupinyinvchartype. with_v); try {for (INT I = 0, Len = hanzichar. length; I <Len; I ++) {If (character. tostring (hanzichar [I]). matches ("[\ u4e00-\ u9fa5] +") {string [] temparr = pinyinhelper. tohanyupinyinstringarray (hanzichar [I], pinyinoutputformat); pinyin. append (temparr [0]);} else {pinyin. append (character. tostring (hanzichar [I]); }} catch (badhanyupinyino Utputformatcombination e) {e. printstacktrace ();} return pinyin. tostring ();}/*** returns the first letter of Chinese pinyin ** @ Param Hanzi * * @ return 文* */protected string getpinyinheadchar (string Hanzi) {stringbuffer pinyinhead = new stringbuffer (); For (INT I = 0, Len = Hanzi. length (); I <Len; I ++) {char tempchar = Hanzi. charat (I); string [] temparr = pinyinhelper. tohanyupinyinstringarray (tempchar); If (null! = Temparr) {pinyinhead. append (temparr [0]. charat (0);} else {pinyinhead. append (tempchar);} return pinyinhead. tostring ();}}

Result:

Haoya! Hehe! Welcome.
Hy! Hh! Welcome.

 

 

The reflection mechanism is called as follows:

 

Package COM. wangzhu. reflect; import Java. lang. reflect. invocationtargetexception; import Java. lang. reflect. method; public class textreflect {/*** @ Param ARGs * @ throws classnotfoundexception * @ throws exceptions * @ throws securityexception * @ throws instantiationexception * @ throws invocationtargetexception * @ throws arguments * /Public stati C void main (string [] ARGs) throws classnotfoundexception, securityexception, nosuchmethodexception, illegalargumentexception, illegalaccessexception, invocationtargetexception, instantiationexception {class <?> Classforname = Class. forname ("com. wangzhu. pinyin. pinyin "); object OBJ = classforname. newinstance (); method [] Methods = classforname. getdeclaredmethods (); For (INT I = 0, Len = methods. length; I <Len; I ++) {system. out. println (methods [I]. getname ();} method = classforname. getdeclaredmethod ("getpinyin", String. class); method. setaccessible (true); // you can access this private method object temp = method only when it is set to true. in Voke (OBJ, "started! Nya! You are also at a glance! Welcome the world! @ "); System. Out. println (temp );}}

Result:

Main
Getpinyin
Getpinyinheadchar
Kaishila! Enya! Zhihuzheyeyiyanzai! Welcome the world! @

 

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.