Convert Chinese into pinyin (full spelling and abbreviations)

Source: Internet
Author: User

The open-source package is used in the program. You can convert Chinese to full spelling or abbreviations. The Code is as follows :/**
* Author: qiaolei
* Create
*/
Package com. Sure;

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 ChineseToEnglish {
// Convert Chinese characters to full spelling
Public static String getPingYin (String src ){

Char [] t1 = null;
T1 = src. toCharArray ();
String [] t2 = new String [t1.length];
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 ++)
{
// Determine whether it is a Chinese character
If (java. lang. Character. toString (t1 [I]). matches ("[\ u4E00-\ u9FA5] + "))
{
T2 = PinyinHelper. toHanyuPinyinStringArray (t1 [I], t3 );
T4 + = t2 [0];
}
Else
T4 + = java. lang. Character. toString (t1 [I]);
}
// System. out. println (t4 );
Return t4;
}
Catch (BadHanyuPinyinOutputFormatCombination e1 ){
E1.printStackTrace ();
}
Return t4;
}

// Returns the first letter of a Chinese character.
Public static String getPinYinHeadChar (String str ){

String convert = "";
For (int j = 0; j <str. length (); j ++ ){
Char word = str. charAt (j );
String [] pinyinArray = PinyinHelper. toHanyuPinyinStringArray (word );
If (pinyinArray! = Null ){
Convert + = pinyinArray [0]. charAt (0 );
} Else {
Convert + = word;
}
}
Return convert;
}
// Transfer the string to an ASCII code
Public static String getCnASCII (String cnStr)
{
StringBuffer strBuf = new StringBuffer ();
Byte [] bGBK = cnStr. getBytes ();
For (int I = 0; I <bGBK. length; I ++ ){
// System. out. println (Integer. toHexString (bGBK [I] & 0xff ));
StrBuf. append (Integer. toHexString (bGBK [I] & 0xff ));
}
Return strBuf. toString ();
} Public static void main (String [] args ){
System. out. println (getPingYin ("Qijiang county "));
System. out. println (getPinYinHeadChar ("Qijiang county "));
System. out. println (getCnASCII ("Qijiang county "));
} The result is as follows: qijiangxian
Qjx
F4ebbdadcfd8

This article from "Qiao Lei's blog learning progress" blog, please be sure to keep this source http://sucre.blog.51cto.com/1084905/353603

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.