dev
This commit is contained in:
parent
d0cb4d6a62
commit
c64bed4efa
6
pom.xml
6
pom.xml
|
|
@ -93,6 +93,12 @@
|
||||||
<artifactId>fastjson</artifactId>
|
<artifactId>fastjson</artifactId>
|
||||||
<version>2.0.38</version>
|
<version>2.0.38</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.belerweb</groupId>
|
||||||
|
<artifactId>pinyin4j</artifactId>
|
||||||
|
<version>2.5.0</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
<artifactId>springfox-swagger2</artifactId>
|
<artifactId>springfox-swagger2</artifactId>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,68 @@
|
||||||
|
package com.syjiaer.clinic.server.common.util;
|
||||||
|
|
||||||
|
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.exception.BadHanyuPinyinOutputFormatCombination;
|
||||||
|
|
||||||
|
|
||||||
|
public class PinYinUtil {
|
||||||
|
public static String getPinyinFull(String inputString) {
|
||||||
|
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
||||||
|
format.setCaseType(HanyuPinyinCaseType.LOWERCASE); // 小写
|
||||||
|
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不带声调
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (char c : inputString.toCharArray()) {
|
||||||
|
if (Character.isWhitespace(c)) {
|
||||||
|
continue; // 跳过空格
|
||||||
|
}
|
||||||
|
if (c > 127 || (c >= '\u4E00' && c <= '\u9FA5')) { // 中文字符
|
||||||
|
try {
|
||||||
|
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
|
||||||
|
if (pinyinArray != null) {
|
||||||
|
sb.append(pinyinArray[0]);
|
||||||
|
}
|
||||||
|
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sb.append(c); // 非中文字符直接追加
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取字符串的拼音首字母
|
||||||
|
*
|
||||||
|
* @param inputString 输入字符串
|
||||||
|
* @return 拼音首字母字符串
|
||||||
|
*/
|
||||||
|
public static String getPinyinFirstLetters(String inputString) {
|
||||||
|
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
||||||
|
format.setCaseType(HanyuPinyinCaseType.UPPERCASE); // 大写
|
||||||
|
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); // 不带声调
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (char c : inputString.toCharArray()) {
|
||||||
|
if (Character.isWhitespace(c)) {
|
||||||
|
continue; // 跳过空格
|
||||||
|
}
|
||||||
|
if (c > 127 || (c >= '\u4E00' && c <= '\u9FA5')) { // 中文字符
|
||||||
|
try {
|
||||||
|
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
|
||||||
|
if (pinyinArray != null) {
|
||||||
|
sb.append(pinyinArray[0].charAt(0)); // 取第一个字母
|
||||||
|
}
|
||||||
|
} catch (BadHanyuPinyinOutputFormatCombination e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sb.append(c); // 非中文字符直接追加
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue