亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Java漢字轉(zhuǎn)拼音工具類完整代碼實例

瀏覽:85日期:2022-09-04 10:30:47

添加依賴

<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)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 青草久久精品亚洲综合专区 | 在线小视频国产 | 欧美深夜福利视频 | 99久久婷婷国产综合精品电影 | 亚洲欧洲视频在线观看 | 国产亚洲精品不卡在线 | 亚洲综合资源 | 91爱国产| 三级毛片在线播放 | 久久99精品国产一区二区三区 | 亚洲限制级 | 免费的很黄很色的床小视频 | 国产精品久久久久久久hd | 污影院 | xxxxchina麻豆免费视频 | 国产精品99r8免费视频2022 | 亚洲国产精品久久久久秋霞66 | 亚洲免费不卡 | 国产精品久久久久9999高清 | 可以看的黄色网址 | 黄色小视频在线播放 | 在线观看精品福利片香蕉 | 一区 在线播放 | 日本高清视频www夜色资源网 | 免费黄色a | 国产91区精品福利在线社区 | 国产美女免费网站 | 在线国产一区 | 伊人久久青青 | 中文字幕另类 | 国产精品成人观看视频网站 | 国产一区二区三区精品久久呦 | 黄色在线免费观看视频 | 国产三级精品在线观看 | 成人午夜精品久久久久久久小说 | 亚洲国产女人aaa毛片在线 | 欧美精品成人一区二区视频一 | 国产精品免费小视频 | 欧美日批 | 国产成人在线免费 | 青草香蕉精品视频在线观看 |