獲取中文的完整拼音並輸出
阿新 • • 發佈:2017-09-22
stat rtert bin tca spa nat ++ () one
1.添加maven依賴
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.0</version> </dependency>
2.獲取拼音邏輯
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++) { String s= String.valueOf(nameChar[i]); if (s.matches("[\\u4e00-\\u9fa5]")) { try { String[] mPinyinArray = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat); pinyinName += mPinyinArray[0]; } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } }else { pinyinName += nameChar[i]; } } return pinyinName; }
獲取中文的完整拼音並輸出