java中文拼音_Java汉字转成汉语拼音工具类

1 importnet.sourceforge.pinyin4j.PinyinHelper;2 importnet.sourceforge.pinyin4j.format.HanyuPinyinCaseType;3 importnet.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;4 importnet.sourceforge.pinyin4j.format.HanyuPinyinToneType;5 importnet.sourceforge.pinyin4j.format.HanyuPinyinVCharType;6 importnet.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;7

8 public classHanyuPinyinHelper {9

10 /**

11 * 将文字转为汉语拼音12 *@paramchineselanguage 要转成拼音的中文13 */

14 publicString toHanyuPinyin(String ChineseLanguage){15 char[] cl_chars =ChineseLanguage.trim().toCharArray();16 String hanyupinyin = "";17 HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormat();18 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//输出拼音全部小写

19 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不带声调

20 defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V) ;21 try{22 for (int i=0; i

24 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0];25 } else {//如果字符不是中文,则不转换

26 hanyupinyin +=cl_chars[i];27 }28 }29 } catch(BadHanyuPinyinOutputFormatCombination e) {30 System.out.println("字符不能转成汉语拼音");31 }32 returnhanyupinyin;33 }34

35 public staticString getFirstLettersUp(String ChineseLanguage){36 returngetFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE);37 }38

39 public staticString getFirstLettersLo(String ChineseLanguage){40 returngetFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE);41 }42

43 public staticString getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) {44 char[] cl_chars =ChineseLanguage.trim().toCharArray();45 String hanyupinyin = "";46 HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormat();47 defaultFormat.setCaseType(caseType);//输出拼音全部大写

48 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不带声调

49 try{50 for (int i = 0; i < cl_chars.length; i++) {51 String str =String.valueOf(cl_chars[i]);52 if (str.matches("[\u4e00-\u9fa5]+")) {//如果字符是中文,则将中文转为汉语拼音,并取第一个字母

53 hanyupinyin += PinyinHelper.toHanyuPinyinStringArray(cl_chars[i], defaultFormat)[0].substring(0, 1);54 } else if (str.matches("[0-9]+")) {//如果字符是数字,取数字

55 hanyupinyin +=cl_chars[i];56 } else if (str.matches("[a-zA-Z]+")) {//如果字符是字母,取字母

57 hanyupinyin +=cl_chars[i];58 } else {//否则不转换

59 hanyupinyin += cl_chars[i];//如果是标点符号的话,带着

60 }61 }62 } catch(BadHanyuPinyinOutputFormatCombination e) {63 System.out.println("字符不能转成汉语拼音");64 }65 returnhanyupinyin;66 }67

68 public staticString getPinyinString(String ChineseLanguage){69 char[] cl_chars =ChineseLanguage.trim().toCharArray();70 String hanyupinyin = "";71 HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormat();72 defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//输出拼音全部大写

73 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不带声调

74 try{75 for (int i = 0; i < cl_chars.length; i++) {76 String str =String.valueOf(cl_chars[i]);77 if (str.matches("[\u4e00-\u9fa5]+")) {//如果字符是中文,则将中文转为汉语拼音,并取第一个字母

78 hanyupinyin +=PinyinHelper.toHanyuPinyinStringArray(79 cl_chars[i], defaultFormat)[0];80 } else if (str.matches("[0-9]+")) {//如果字符是数字,取数字

81 hanyupinyin +=cl_chars[i];82 } else if (str.matches("[a-zA-Z]+")) {//如果字符是字母,取字母

83

84 hanyupinyin +=cl_chars[i];85 } else {//否则不转换

86 }87 }88 } catch(BadHanyuPinyinOutputFormatCombination e) {89 System.out.println("字符不能转成汉语拼音");90 }91 returnhanyupinyin;92 }93 /**

94 * 取第一个汉字的第一个字符95 * @Title: getFirstLetter96 * @Description: TODO97 *@returnString98 *@throws

99 */

100 public staticString getFirstLetter(String ChineseLanguage){101 char[] cl_chars =ChineseLanguage.trim().toCharArray();102 String hanyupinyin = "";103 HanyuPinyinOutputFormat defaultFormat = newHanyuPinyinOutputFormat();104 defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);//输出拼音全部大写

105 defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);//不带声调

106 try{107 String str = String.valueOf(cl_chars[0]);108 if (str.matches("[\u4e00-\u9fa5]+")) {//如果字符是中文,则将中文转为汉语拼音,并取第一个字母

109 hanyupinyin =PinyinHelper.toHanyuPinyinStringArray(110 cl_chars[0], defaultFormat)[0].substring(0, 1);;111 } else if (str.matches("[0-9]+")) {//如果字符是数字,取数字

112 hanyupinyin += cl_chars[0];113 } else if (str.matches("[a-zA-Z]+")) {//如果字符是字母,取字母

114

115 hanyupinyin += cl_chars[0];116 } else {//否则不转换

117

118 }119 } catch(BadHanyuPinyinOutputFormatCombination e) {120 System.out.println("字符不能转成汉语拼音");121 }122 returnhanyupinyin;123 }124

125 public static voidmain(String[] args) {126 HanyuPinyinHelper hanyuPinyinHelper = newHanyuPinyinHelper() ;127 System.out.println(hanyuPinyinHelper.toHanyuPinyin("多发的发独守空房阿道夫打发第三方"));128 }129 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值