Java to realize Chinese pinyin conversion

Source: Internet
Author: User
Tags stringbuffer

Pinyin4j-2.5.0.jar such a jar package was used;
The specific implementation code is as follows: 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 Hanzitopinyin {/** * converts Chinese characters to full spell * * @param src * @return String */public static string Getpinyin (String src)
{char[] T1 = null; t1 = Src.tochararray ();//System.out.println (t1.length);
string[] t2 = new String[t1.length];
System.out.println (t2.length);
Set the format of Chinese pinyin output hanyupinyinoutputformat t3 = new Hanyupinyinoutputformat ();
T3.setcasetype (hanyupinyincasetype.lowercase);
T3.settonetype (Hanyupinyintonetype.without_tone);
T3.setvchartype (HANYUPINYINVCHARTYPE.WITH_V);
String t4 = "";
int t0 = t1.length; try {for (int i = 0; i < t0; i++) {//To determine if it is a kanji character
System.out.println (T1[i]); if (character.tostring (T1[i]). Matches ("[\\u4e00-\\u9fa5]+")) {t2 = Pinyinhelper.tohanyupinyinstringarray (t1[i], T3) //A few of the Chinese characters are all spelled in the T2 array T4 + + t2[0];//take out the first pronunciation of the Chinese spelling and connect to the string t4 after} else {//If not kanji characters, directly remove the character and connect to the string t4 after T4 + = character.tostring
(T1[i]); catch (Badhanyupinyinoutputformatcombination e) {//TODO auto-generated catch block E.printstacktrace ();
4.toUpperCase (); /** * Extracts the first letter of each kanji * * @param str * @return string/public static string Getpinyinheadchar (String str) {string conve
RT = ""; for (int j = 0; J < Str.length (); j + +) {char word = Str.charat (j);//extract the first letter of Chinese characters string[] Pinyinarray = Pinyinhelper.toh
Anyupinyinstringarray (word);
if (Pinyinarray!= null) {convert + = Pinyinarray[0].charat (0);} else {convert + = Word;}}
return Convert.touppercase ();  /** * Converts string to ASCII * * @param cnstr * @return string/public static string Getcnascii (String cnstr) {StringBuffer
Strbuf = new StringBuffer (); Convert string to byte sequence byte[] BGBK = Cnstr.getbytes (); for (int i = 0; i < bgbk.length i++) {//System.out.println (Integer.tohexstring (bgbk[i) & 0xff);//convert each character to ASci
I code strbuf.append (integer.tohexstring (bgbk[i) & 0xff));
return strbuf.tostring (); }
}


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.