Java获取中文汉字的拼音

Java获取中文汉字的拼音

参考地址:https://www.cnblogs.com/langtianya/p/7098089.html

示例代码:

 1 package com.tgram.sboot.util;
 2 
 3 import net.sourceforge.pinyin4j.PinyinHelper;
 4 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
 5 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
 6 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
 7 import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
 8 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 9 
10 public class ConvertSpellUtil
11 {
12     /**
13      * 传入中文汉字字符串,转换出对应拼音字符串
14      *   注:如果传入的中文中有任一同音字都会返回字符串信息:false
15      * @param src 中文汉字字符串
16      * @param hasBlank 是否需要有空格
17      * @return 对应拼音字符串
18      */
19     public static String convertToSpell(String src,boolean hasBlank)
20     {
21         char[] t1 = src.toCharArray();
22         String[] t2 ;
23         // 设置汉字拼音输出的格式
24         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
25         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
26         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
27         t3.setVCharType(HanyuPinyinVCharType.WITH_V);
28         StringBuilder t4 = new StringBuilder();
29         int t0 = t1.length;
30         try
31         {
32             for (int i = 0; i < t0; i++)
33             {
34                 // 判断能否为汉字字符
35                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+"))
36                 {
37             // 将汉字的几种全拼都存到t2数组中,由于汉字会有多音字,所以就只取第一个元素
38                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
39                     if (t2 != null)
40                     {
41                         if(!hasBlank)
42                         {
43                             t4.append(t2[0]); // 取数组第一个元素拼接成拼音字符串
44                         }
45                         else
46                         {
47                             if(i == 0 || i == t0 - 1)
48                             {
49                                 t4.append(t2[0]); //第一个和最后一个不加空格
50                             }
51                             else
52                             {
53                                 t4.append(" ").append(t2[0]);
54                             }
55                         }
56                     }
57                 }
58                 else
59                 {
60                     // 如果不是汉字字符,间接取出字符并连接到字符串t4后
61                     t4.append(Character.toString(t1[i]));
62                 }
63             }
64         }
65         catch (BadHanyuPinyinOutputFormatCombination e)
66         {
67             e.printStackTrace();
68         }
69 
70         // 返回拼音字符串(如果全是大写的非中文的字符可以转成全小写,具体看需要)
71         return t4.toString().toLowerCase();
72     }
73 
74     public static void main(String[] args)
75     {
76         // 结果:false: diedaimeigehanzidepinyin!!!
77         //        true: die dai meige han zi de pin yin!!!
78         String polyphone = ConvertSpellUtil.convertToSpell("迭代每ge汉字的拼音!!!",true);
79         System.out.println(polyphone);
80 
81         // 结果:全字母true和false结果一样:    helloworld,i love java!!!
82         String polyphone2 = ConvertSpellUtil
83                   .convertToSpell("    HELLOWORLD,I LOVE JAVA!!!     ",true);
84         System.out.println(polyphone2);
85 
86         // 结果:true结果:     ni hao shi jie,i love java!!!
87         //      false结果:    nihaoshijie,i love java!!!
88         String polyphone3 = ConvertSpellUtil
89                   .convertToSpell("    你好世界,I LOVE JAVA!!!     ",false);
90         System.out.println(polyphone3);
91     }
92 }

猜你喜欢

转载自www.cnblogs.com/jason2018524/p/10271065.html
今日推荐