Java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例
更新時間:2020年03月20日 09:04:05 作者:風(fēng)繾云流
這篇文章主要介紹了java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
添加依賴
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
工具類代碼:
public class PinYinUtils {
public static HanyuPinyinOutputFormat PINYIN_FORMAT;
static {
PINYIN_FORMAT = new HanyuPinyinOutputFormat();
/**
* 大小寫設(shè)置
* LOWERCASE:小寫
* UPPERCASE:大寫
*/
PINYIN_FORMAT.setCaseType(HanyuPinyinCaseType.LOWERCASE);
/**
* 輸出音標(biāo)設(shè)置
*
* WITH_TONE_MARK:直接用音標(biāo)符(VCharType必須設(shè)置WITH_U_UNICODE,否則會拋出異常)
* WITH_TONE_NUMBER:1-4數(shù)字表示音標(biāo)
* WITHOUT_TONE:沒有音標(biāo)
*/
PINYIN_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
/**
* 特殊音符ü的設(shè)置
* WITH_U_AND_COLON:用u表示(沒有設(shè)置默認(rèn)用u表示)
* WITH_V:用v表示
* WITH_U_UNICODE:用ü表示
*/
PINYIN_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);
}
/**
* 取漢字的拼音首字母
* @param chinese
* @return
*/
public static String toFirstPinYin(String chinese){
StringBuilder result = new StringBuilder();
//將字符串轉(zhuǎn)成字符數(shù)組
char[] chars = chinese.toCharArray();
try {
for (char c : chars) {
//是中文則進(jìn)行轉(zhuǎn)換
if(String.valueOf(c).matches("[\u4e00-\u9fa5]+")){
String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
result.append(pinyinStr[0].charAt(0));//取每個中文的第一個拼音字母
}else {
result.append(c);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
return result.toString();
}
/**
* 漢字轉(zhuǎn)拼音小寫
* @param chinese
* @return
*/
public static String toPinYin(String chinese){
//創(chuàng)建返回對象
StringBuilder result = new StringBuilder();//方法調(diào)用的時候新建,對象沒有共享,不會有線程安全問題。
//將字符串轉(zhuǎn)成字符數(shù)組
char[] chars = chinese.toCharArray();
try {
for (char c : chars) {
//是中文則進(jìn)行轉(zhuǎn)換
if(String.valueOf(c).matches("[\u4e00-\u9fa5]+")){
String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
// result.append(pinyinStr[0].charAt(0));//取每個中文的第一個拼音字母
result.append(pinyinStr[0]);
}else {
result.append(c);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
return result.toString();
}
/**
* 漢字轉(zhuǎn)拼音每個字符串的第一個字母大寫其余小寫
* @param chinese
* @return
*/
public static String toUpperStringsFirstCharPinYin(String chinese){
//創(chuàng)建返回對象
StringBuilder result = new StringBuilder();//方法調(diào)用的時候新建,對象沒有共享,不會有線程安全問題。
//將字符串轉(zhuǎn)成字符數(shù)組
char[] chars = chinese.toCharArray();
try {
for (char c : chars) {
//是中文則進(jìn)行轉(zhuǎn)換
if(String.valueOf(c).matches("[\u4e00-\u9fa5]+")){
String[] pinyinStr = PinyinHelper.toHanyuPinyinStringArray(c, PINYIN_FORMAT);
// result.append(pinyinStr[0].charAt(0));//取每個中文的第一個拼音字母
String c1 = String.valueOf(pinyinStr[0]);
result.append(c1.substring(0,1).toUpperCase()).append(c1.substring(1));
}else {
result.append(c);
}
}
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
}
return result.toString();
}
public static void main(String[] args) {
//測試StringBuilder是否有線程安全問題
String str = "拼音工具lv";
String pinYin = toUpperStringsFirstCharPinYin(str);
System.out.println(pinYin);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中JSONObject與JSONArray的使用示例小結(jié)
JSONObject-lib包是一個beans,collections,maps,java arrays和xml和JSON互相轉(zhuǎn)換的包,本文給大家介紹Java中JSONObject與JSONArray的使用示例小結(jié),感興趣的朋友一起看看吧2025-02-02
spring-spring容器中bean知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于spring-spring容器中bean知識點(diǎn)總結(jié),有需要的朋友們可以學(xué)習(xí)下。2019-08-08
NoHttpResponseException異常解決優(yōu)化HttpClient配置以避免連接問題
這篇文章主要為大家介紹了NoHttpResponseException異常解決,優(yōu)化HttpClient配置以避免連接問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10

