利用pinyin4j实现汉语转拼音。实例代码如下:
public static void chToPinyin() {
String ch = "a张#!!@##!!!上1";
int len = ch.length();
for (int i = 0; i < len; i++) {
String zh = charChToPinyin(ch.charAt(i));
if (zh != null && zh.length() > 0) {
System.out.print(zh);
} else {
System.out.print(ch.charAt(i));
}
}
}
public static String charChToPinyin(char paramAnonymousChar) {
String[] strs = PinyinHelper.toHanyuPinyinStringArray(paramAnonymousChar);
if (strs != null && strs.length > 0) {
String str = strs[0];
return str.substring(0, str.length() - 1).toUpperCase();
}
return "";
}