优化
This commit is contained in:
@ -169,6 +169,11 @@
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.belerweb</groupId>
|
||||
<artifactId>pinyin4j</artifactId>
|
||||
<version>2.5.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,40 @@
|
||||
package com.ruoyi.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;
|
||||
|
||||
public class PinYinUtil {
|
||||
|
||||
public static String getChineseNameInitials(String chineseName) {
|
||||
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
||||
// 设置拼音大小写格式为大写
|
||||
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
|
||||
// 设置拼音声调格式为无音调
|
||||
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||
// 设置 ü 的显示格式
|
||||
format.setVCharType(HanyuPinyinVCharType.WITH_V);
|
||||
|
||||
StringBuilder initials = new StringBuilder();
|
||||
for (char c : chineseName.toCharArray()) {
|
||||
if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) {
|
||||
try {
|
||||
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
|
||||
if (pinyinArray != null) {
|
||||
// 取拼音的首字母
|
||||
initials.append(pinyinArray[0].charAt(0));
|
||||
}
|
||||
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
// 非中文字符直接添加
|
||||
initials.append(c);
|
||||
}
|
||||
}
|
||||
return initials.toString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user