1:在项目 POM 中 引入 汉字拼音转换JAR包
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
2:工具类
public static String getPinYinHeadChar(String str) {
if (isNull(str)) {
return "";
}
StringBuilder convert = new StringBuilder();
for (int j = IntegerPool.ZERO; j < str.length(); j++) {
char word = str.charAt(j);
// 提取汉字的首字母
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
if (pinyinArray != null) {
convert.append(pinyinArray[IntegerPool.ZERO].charAt(IntegerPool.ZERO));
}
else {
convert.append(word);
}
}
convert = new StringBuilder(stringAllTrim(convert.toString()));
return