请注意: 该util目前只是将汉字转为拼音,并无特殊功能,例如将"银行"是转为"yinxing",而不是"yinhang"
使用pinyin4j-2.5.1.jar commons-lang3-3.7.jar
maven依赖为
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
代码如下:
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 zad
* @date 2020/2/14 17:10
* @description
*/
public class PinYinUtil {
private static final HanyuPinyinOutputFormat PINYIN_OUTPUT_FORMAT = new HanyuPinyinOutputFormat();
private static final String EMPTY = "";
static {
PINYIN_OUTPUT_FORMAT.setCaseType(HanyuPinyinCaseType.LOWERCASE);
PINYIN_OUTPUT_FORMAT.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
PINYIN_OUTPUT_FORMAT.setVCharType(HanyuPinyinVCharType.WITH_V);
}
private PinYinUtil() {
}
public static String getPinYin(String chineseCharacter) {
return getPinYin(chineseCharacter, false);
}
public static String getPinYinHeadChar(String chineseCharacter) {
return getPinYin(chineseCharacter, true);
}
private static String getPinYin(String raw, boolean onlyHeader) {
if (StringUtil.isBlank(raw)) {
return EMPTY;
}
StringBuilder sb = new StringBuilder();
for (char cc : raw.toCharArray()) {
if (addPinYin(sb, cc, onlyHeader)) {
//
} else {
addLetterOrNumber(sb, cc);
}
}
return sb.toString();
}
private static boolean addLetterOrNumber(StringBuilder sb, char raw) {
if (isCharacterOrDigits(raw)) {
sb.append(raw);
return true;
}
return false;
}
private static boolean addPinYin(StringBuilder sb, char c, boolean onlyHeader) {
if (isChineseCharacter(c)) {
if (onlyHeader) {
sb.append(getPinYinHeader(c));
} else {
sb.append(getPinYin(c));
}
return true;
}
return false;
}
private static char getPinYinHeader(char raw) {
String res = doGetPinYinString(raw);
if (StringUtil.isNotEmpty(res)) {
return res.charAt(0);
}
return Character.MIN_VALUE;
}
private static String getPinYin(char raw) {
return StringUtil.defaultIfEmpty(doGetPinYinString(raw), EMPTY);
}
private static String doGetPinYinString(char cc) {
try {
String[] res = PinyinHelper.toHanyuPinyinStringArray(cc, PINYIN_OUTPUT_FORMAT);
return res[0];
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
// do nothing
}
return EMPTY;
}
private static boolean isChineseCharacter(char c) {
return Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN;
}
private static boolean isCharacterOrDigits(char c) {
return Character.isLetterOrDigit(c);
}
}