import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
public class ChineseToPinyinUtil {
/**
* 汉字转拼音的方法
* @param name 汉字
* @return 拼音
*/
public static String HanyuToPinyin(String name){
if(name == null || name.trim().equals(""))
return "";
String pinyinName = "";
char[] nameChar = name.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 (Exception e) {
e.printStackTrace();
}
}else{
pinyinName += nameChar[i];
}
}
return pinyinName;
}
@SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println(new ChineseToPinyinUtil().HanyuToPinyin("%-w啊ad"));
}
}
by Alice