Package Com.jframe.kit;
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.exception.BadHanyuPinyinOutputFormatCombination;
/**
* Chinese character conversion bit Hanyu Pinyin, English characters unchanged
* @author Xuke
*
*/
public class Cn2spellkit {
/**
* Chinese characters converted to initials, English characters unchanged
* @param chines Kanji
* @return Pinyin
*/
public static string Convertertofirstspell (String chines) {
String pinyinname = "";
char[] Namechar = Chines.tochararray ();
Hanyupinyinoutputformat DefaultFormat = new Hanyupinyinoutputformat ();
Defaultformat.setcasetype (hanyupinyincasetype.lowercase);
Defaultformat.settonetype (Hanyupinyintonetype.without_tone);
for (int i = 0; i < namechar.length; i++) {
if (Namechar[i] > 128) {
try {
Pinyinname + = Pinyinhelper.tohanyupinyinstringarray (Namechar[i], DefaultFormat) [0].charat (0);
} catch (Badhanyupinyinoutputformatcombination e) {
E.printstacktrace ();
}
}else{
Pinyinname + = Namechar[i];
}
}
return pinyinname;
}
/**
* Chinese character conversion bit Hanyu Pinyin, English characters unchanged
* @param chines Kanji
* @return Pinyin
*/
public static string Convertertospell (String chines) {
String pinyinname = "";
char[] Namechar = Chines.tochararray ();
Hanyupinyinoutputformat DefaultFormat = new Hanyupinyinoutputformat ();
Defaultformat.setcasetype (hanyupinyincasetype.lowercase);
Defaultformat.settonetype (Hanyupinyintonetype.without_tone);
for (int i = 0; i < namechar.length; i++) {
if (Namechar[i] > 128) {
try {
Pinyinname + = Pinyinhelper.tohanyupinyinstringarray (Namechar[i], DefaultFormat) [0];
} catch (Badhanyupinyinoutputformatcombination e) {
E.printstacktrace ();
}
}else{
Pinyinname + = Namechar[i];
}
}
return pinyinname;
}
public static string Spell (string name) {
String pinyinname = "";
if (name!=null &&! "". Equals (name)) {
if (Name.length () <=2) {
Pinyinname = Convertertospell (name);
}else{
Pinyinname = Convertertospell (name.substring (0,1)) +convertertofirstspell (Name.substring (1,name.length ()));
}
}
return pinyinname;
}
public static void Main (string[] args) {
System.out.println (Spell ("Yang Cheng"));
System.out.println (Spell ("Wang Shuai (small)"). toUpperCase ());
}
}
Must Pinyin4j-2.5.0.jar
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Translate Java text into pinyin