添加依賴java
<dependency> <groupId>com.github.stuxuhai</groupId> <artifactId>jpinyin</artifactId> <version>1.1.8</version> </dependency>
測試代碼git
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"); // 添加用戶自定義字典 }