java中汉字自动转换成拼音
发布日期:2021-08-21 02:35:30 浏览次数:20 分类:技术文章

本文共 3155 字,大约阅读时间需要 10 分钟。

1.需要下载jar包 pinyin4j.2.5.0.jar ,加入到WEB-INF下的lib里边,右键add to bulid path。

2.[代码]PinYinUtil  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package
oa.common.utils;
 
import
net.sourceforge.pinyin4j.PinyinHelper;
import
net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import
net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import
net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import
net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import
net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
 
/**
 
* 拼音工具类
 
*
 
* @author lsf
 
*/
public
class
PinYinUtil {
    
/**
     
* 将字符串中的中文转化为拼音,其他字符不变
     
*
     
* @param inputString
     
* @return
     
*/
    
public
static
String getPingYin(String inputString) {
        
HanyuPinyinOutputFormat format =
new
HanyuPinyinOutputFormat();
        
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        
format.setVCharType(HanyuPinyinVCharType.WITH_V);
 
        
char
[] input = inputString.trim().toCharArray();
        
String output =
""
;
 
        
try
{
            
for
(
int
i =
0
; i < input.length; i++) {
                
if
(java.lang.Character.toString(input[i]).matches(
"[\\u4E00-\\u9FA5]+"
)) {
                    
String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                    
output += temp[
0
];
                
}
else
                    
output += java.lang.Character.toString(input[i]);
            
}
        
}
catch
(BadHanyuPinyinOutputFormatCombination e) {
            
e.printStackTrace();
        
}
        
return
output;
    
}
    
/** 
     
* 获取汉字串拼音首字母,英文字符不变 
     
* @param chinese 汉字串 
     
* @return 汉语拼音首字母 
     
*/  
    
public
static
String getFirstSpell(String chinese) {  
            
StringBuffer pybf =
new
StringBuffer();  
            
char
[] arr = chinese.toCharArray();  
            
HanyuPinyinOutputFormat defaultFormat =
new
HanyuPinyinOutputFormat();  
            
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
            
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
            
for
(
int
i =
0
; i < arr.length; i++) {  
                    
if
(arr[i] >
128
) {  
                            
try
{  
                                    
String[] temp = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);  
                                    
if
(temp !=
null
) {  
                                            
pybf.append(temp[
0
].charAt(
0
));  
                                    
}  
                            
}
catch
(BadHanyuPinyinOutputFormatCombination e) {  
                                    
e.printStackTrace();  
                            
}  
                    
}
else
{  
                            
pybf.append(arr[i]);  
                    
}  
            
}  
            
return
pybf.toString().replaceAll(
"\\W"
,
""
).trim();  
    
}  
    
/** 
     
* 获取汉字串拼音,英文字符不变 
     
* @param chinese 汉字串 
     
* @return 汉语拼音 
     
*/  
    
public
static
String getFullSpell(String chinese) {  
            
StringBuffer pybf =
new
StringBuffer();  
            
char
[] arr = chinese.toCharArray();  
            
HanyuPinyinOutputFormat defaultFormat =
new
HanyuPinyinOutputFormat();  
            
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
            
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
            
for
(
int
i =
0
; i < arr.length; i++) {  
                    
if
(arr[i] >
128
) {  
                            
try
{  
                                    
pybf.append(PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat)[
0
]);  
                            
}
catch
(BadHanyuPinyinOutputFormatCombination e) {  
                                    
e.printStackTrace();  
                            
}  
                    
}
else
{  
                            
pybf.append(arr[i]);  
                    
}  
            
}  
            
return
pybf.toString();  
    

3.用到的时候,import com.XXX.PinYinUtil; 看自己的实际路径

然后调用PinYinUtil.getFirstSpell(String chinese);

           PinYinUtil.getFullSpell(String chinese);

转载地址:https://blog.csdn.net/weixin_33708432/article/details/86329328 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:imx6 fec分析
下一篇:MFS文件系统

发表评论

最新留言

很好
[***.229.124.182]2024年03月23日 08时05分44秒