博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java汉字转拼音以及得到首字母通用方法
阅读量:4325 次
发布时间:2019-06-06

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

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
PingYinUtil {
    
/**
     
* 将字符串中的中文转化为拼音,其他字符不变
     
*
     
* @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();  
    

转载于:https://www.cnblogs.com/duanxz/p/4193641.html

你可能感兴趣的文章
Ural1297 最长回文子串(后缀数组+RMQ)
查看>>
十九、CSS如何引入字体
查看>>
DS博客作业07--查找
查看>>
c# Invalidate() Update() Refresh()的区别
查看>>
work of 1/5/2016
查看>>
自己做了个微信小程序
查看>>
CMD获取当前目录的绝对路径
查看>>
HTML5新规范和CSS3新特性
查看>>
使用php后台给自己做一个页面路由,配合ajax实现局部刷新。
查看>>
类与对象(二)
查看>>
NSString 的常用方法
查看>>
mysql的engine不同,导致事物回滚失败的问题
查看>>
JAVAWeb使用POI做导出Excel
查看>>
今天解决了首页无头像被显示的问题
查看>>
charts 画折线图
查看>>
[py]__name__ 属于哪个文件
查看>>
技术分析淘宝的超卖宝贝
查看>>
i++和++1
查看>>
react.js
查看>>
实验四【bx】和loop的使用
查看>>