Java 汉字获取拼音或首字母工具类
- 期望结果
input:时间
output: shijian
或sj
1、Maven依赖
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
2、工具类代码
package com.spacevast.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: PengCheng.Wu
* @Date: 2021/8/26 12:57
* @Description: <拼音工具类>
*/
public class PinYinUtils {
/**
* 将字符串中的中文转化为拼音,其他字符不变
*
* @param inputString
* @return
*/
public static String getPinYin(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();
StringBuilder output = new StringBuilder();
try {
for (char c : input) {
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format);
output.append(temp[0]);
} else {
output.append(c);
}
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return output.toString();
}
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音首字母
*/
public static String getFirstSpell(String chinese) {
StringBuilder pybf = new StringBuilder();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (char c : arr) {
if (c > 128) {
try {
String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat);
if (temp != null) {
pybf.append(temp[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(c);
}
}
return pybf.toString().replaceAll("\\W", "").trim();
}
/**
* 获取第一个汉字串拼音首字母,英文字符不变
*
* @param chinese 汉字串
* @return 第一个汉字的拼音大写首字母
*/
public static String getFirstPy(String chinese) {
String firstSpell = getFirstSpell(chinese);
return "".equals(firstSpell) ? firstSpell : firstSpell.substring(0, 1).toUpperCase();
}
/**
* 获取汉字串拼音,英文字符不变
*
* @param chinese 汉字串
* @return 汉语拼音
*/
public static String getFullSpell(String chinese) {
StringBuilder pybf = new StringBuilder();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (char c : arr) {
if (c > 128) {
try {
pybf.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
pybf.append(c);
}
}
return pybf.toString();
}
}
.END
给个一键三连吧~