Java漢字轉(zhuǎn)拼音工具類完整代碼實例
添加依賴
<dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version></dependency>
工具類代碼:
public class PinYinUtils { public static HanyuPinyinOutputFormat PINYIN_FORMAT; static { PINYIN_FORMAT = new HanyuPinyinOutputFormat(); /** * 大小寫設(shè)置 * LOWERCASE:小寫 * UPPERCASE:大寫 */ PINYIN_FORMAT.setCaseType(HanyuPinyinCaseType.LOWERCASE); /** * 輸出音標(biāo)設(shè)置 * * WITH_TONE_MARK:直接用音標(biāo)符(VCharType必須設(shè)置WITH_U_UNICODE,否則會拋出異常) * WITH_TONE_NUMBER:1-4數(shù)字表示音標(biāo) * WITHOUT_TONE:沒有音標(biāo) */ PINYIN_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE); /** * 特殊音符ü的設(shè)置 * WITH_U_AND_COLON:用u表示(沒有設(shè)置默認(rèn)用u表示) * WITH_V:用v表示 * WITH_U_UNICODE:用ü表示 */ PINYIN_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE); } /** * 取漢字的拼音首字母 * @param chinese * @return */ public static String toFirstPinYin(String chinese){ StringBuilder result = new StringBuilder(); //將字符串轉(zhuǎn)成字符數(shù)組 char[] chars = chinese.toCharArray(); try { for (char c : chars) {//是中文則進(jìn)行轉(zhuǎn)換if(String.valueOf(c).matches('[u4e00-u9fa5]+')){ String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT); result.append(pinyinStr[0].charAt(0));//取每個中文的第一個拼音字母}else { result.append(c);} } } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } return result.toString(); } /** * 漢字轉(zhuǎn)拼音小寫 * @param chinese * @return */ public static String toPinYin(String chinese){ //創(chuàng)建返回對象 StringBuilder result = new StringBuilder();//方法調(diào)用的時候新建,對象沒有共享,不會有線程安全問題。 //將字符串轉(zhuǎn)成字符數(shù)組 char[] chars = chinese.toCharArray(); try { for (char c : chars) {//是中文則進(jìn)行轉(zhuǎn)換if(String.valueOf(c).matches('[u4e00-u9fa5]+')){ String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);// result.append(pinyinStr[0].charAt(0));//取每個中文的第一個拼音字母 result.append(pinyinStr[0]);}else { result.append(c);} } } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } return result.toString(); } /** * 漢字轉(zhuǎn)拼音每個字符串的第一個字母大寫其余小寫 * @param chinese * @return */ public static String toUpperStringsFirstCharPinYin(String chinese){ //創(chuàng)建返回對象 StringBuilder result = new StringBuilder();//方法調(diào)用的時候新建,對象沒有共享,不會有線程安全問題。 //將字符串轉(zhuǎn)成字符數(shù)組 char[] chars = chinese.toCharArray(); try { for (char c : chars) {//是中文則進(jìn)行轉(zhuǎn)換if(String.valueOf(c).matches('[u4e00-u9fa5]+')){ String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);// result.append(pinyinStr[0].charAt(0));//取每個中文的第一個拼音字母 String c1 = String.valueOf(pinyinStr[0]); result.append(c1.substring(0,1).toUpperCase()).append(c1.substring(1));}else { result.append(c);} } } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) { badHanyuPinyinOutputFormatCombination.printStackTrace(); } return result.toString(); } public static void main(String[] args) { //測試StringBuilder是否有線程安全問題 String str = '拼音工具lv'; String pinYin = toUpperStringsFirstCharPinYin(str); System.out.println(pinYin); }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Jsp中request的3個基礎(chǔ)實踐2. jsp EL表達(dá)式詳解3. XML入門的常見問題(一)4. Django ORM實現(xiàn)按天獲取數(shù)據(jù)去重求和例子5. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實現(xiàn)6. Python多線程操作之互斥鎖、遞歸鎖、信號量、事件實例詳解7. idea設(shè)置自動導(dǎo)入依賴的方法步驟8. idea給項目打war包的方法步驟9. Django程序的優(yōu)化技巧10. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?
