Java changes the Chinese characters to the corresponding first letter

Source: Internet
Author: User
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 {


Converts Chinese characters to full spelling, and other characters are unchanged
public static string Getpinyin (String src) {
src = Src.replaceall ("(", "("). ReplaceAll (")", ")");
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 if the character is Chinese characters
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]);
}
}
return t4;
catch (Badhanyupinyinoutputformatcombination e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return t4;
}


Returns the first letter in Chinese, the English character is unchanged
public static string Getpinyinfirst (String src) {
String temp = "";
String demo = "";
String convert = "";
for (int j = 0; J < Src.length (); j + +) {
Char word = Src.charat (j);
string[] Pinyinarray = Pinyinhelper.tohanyupinyinstringarray (word);
if (Pinyinarray!= null) {
Convert + + pinyinarray[0].charat (0);
} else {
Convert + + word;
}
}
return convert;
Convert is currently a lowercase first letter, and the following is to convert lowercase initials to uppercase
for (int i=0;i<convert.length (); i++) {
if (Convert.charat (i) >= ' a ' &&convert.charat (i) <= ' z ') {
Temp=convert.substring (i, i+1). toUpperCase ();
Demo+=temp;
// }
// }
}


To transfer a string to an ASCII code
public static string Getcnascii (String str) {
StringBuffer sb = new StringBuffer ();
byte[] BGBK = Str.getbytes ();
for (int i = 0; i < bgbk.length; i++) {
Sb.append (Integer.tohexstring (bgbk[i) & 0xff));
}
return sb.tostring ();
}


public static void Main (string[] args) {
String str = "I love you to the People's Republic of China ZR";
SYSTEM.OUT.PRINTLN (Getpinyin (str));
SYSTEM.OUT.PRINTLN (Getpinyinfirst (str));
SYSTEM.OUT.PRINTLN (Getcnascii (str));


}
}

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.