Java汉字转成汉语拼音工具类 -----第一种方式

所需要的pom文件:
<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.1</version>
        </dependency>


import net.sourceforge.pinyin4j.PinyinHelper;
  2 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  3 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  4 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  5 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
  6 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  7 
  8 public class HanyuPinyinHelper {
  9 
 10     /** 
 11      * 将文字转为汉语拼音
 12      * @param chineselanguage 要转成拼音的中文
 13      */
 14     public String toHanyuPinyin(String ChineseLanguage){
 15         char[] cl_chars = ChineseLanguage.trim().toCharArray();
 16         String hanyupinyin = "";
 17         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 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<cl_chars.length; i++){
 23                 if (String.valueOf(cl_chars[i]).matches("[\u4e00-\u9fa5]+")){// 如果字符是中文,则将中文转为汉语拼音
 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         return hanyupinyin;
 33     }
 34     
 35     public static String getFirstLettersUp(String ChineseLanguage){
 36         return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.UPPERCASE);
 37     }
 38     
 39     public static String getFirstLettersLo(String ChineseLanguage){
 40         return getFirstLetters(ChineseLanguage ,HanyuPinyinCaseType.LOWERCASE);
 41     }
 42     
 43     public static String getFirstLetters(String ChineseLanguage,HanyuPinyinCaseType caseType) {
 44         char[] cl_chars = ChineseLanguage.trim().toCharArray();
 45         String hanyupinyin = "";
 46         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 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         return hanyupinyin;
 66     }
 67     
 68     public static String getPinyinString(String ChineseLanguage){
 69         char[] cl_chars = ChineseLanguage.trim().toCharArray();
 70         String hanyupinyin = "";
 71         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 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         return hanyupinyin;
 92     }
 93     /**
 94      * 取第一个汉字的第一个字符
 95     * @Title: getFirstLetter 
 96     * @Description: TODO 
 97     * @return String   
 98     * @throws
 99      */
100     public static String getFirstLetter(String ChineseLanguage){
101         char[] cl_chars = ChineseLanguage.trim().toCharArray();
102         String hanyupinyin = "";
103         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
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         return hanyupinyin;
123     }
124     
125     public static void main(String[] args) {
126         HanyuPinyinHelper hanyuPinyinHelper = new HanyuPinyinHelper() ;
127         System.out.println(hanyuPinyinHelper.toHanyuPinyin("多发的发独守空房阿道夫打发第三方"));
128     }
129 }

猜你喜欢

转载自blog.csdn.net/xiaoyutongxue6/article/details/81943372