添加依赖
com.github.stuxuhai
jpinyin
1.1.8
测试代码
public static void main(String[] args) throws Exception{
String str = "你好世界";
PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_MARK); // nǐ,hǎo,shì,jiè
PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_NUMBER); // ni3,hao3,shi4,jie4
String s = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE);// ni,hao,shi,jie
System.out.println(s);
String shortPinyin = PinyinHelper.getShortPinyin(str);// nhsj
System.out.println(shortPinyin);
//PinyinHelper.addPinyinDict("user.dict"); // 添加用户自定义字典
}