汉语拼音util
Posted zad27
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了汉语拼音util相关的知识,希望对你有一定的参考价值。
使用pinyin4j-2.5.1.jar.
maven依赖为
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.1</version>
</dependency>
代码如下:
package com.zad.JDK8.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.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.ArrayUtils;
import java.util.regex.Pattern;
/**
* 描述:
* 汉字转韩语拼音util
*
* @author zad
* @create 2018-11-01 12:25
*/
public class PinYinUtil {
/**
* 汉语拼音format
*/
private static final HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
/**
* 汉字对应正则表达式 pattern
*/
private static final Pattern chineseCharacterPattern = Pattern.compile("[\u4E00-\u9FA5]");
//字母对应正则
private static final Pattern characterPattern = Pattern.compile("[a-zA-Z]");
static {
hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
}
private PinYinUtil() {
throw new AssertionError("Util禁止反射实例化");
}
/**
* 将chineseCharacter中汉字转为拼音,其余所有字符直接略过,
* 若需要额外处理,则需要额外判断
*
* @param chineseCharacter
* @return
*/
public static String getPingYin(String chineseCharacter) {
StringBuilder sb = new StringBuilder();
try {
for (char cc : chineseCharacter.toCharArray()) {
if (chineseCharacterPattern.matcher(String.valueOf(cc)).matches()) {
sb.append(PinyinHelper.toHanyuPinyinStringArray(cc, hanyuPinyinOutputFormat)[0]);
}
/* // 如果是字母,直接添加
if(characterPattern.matcher(String.valueOf(cc)).matches())){
sb.append(cc);
}*/
}
} catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return sb.toString();
}
/**
* 将chineseCharacter中汉字转为拼音并获取首字母,其余所有字符直接略过,
*
* @param chineseCharacter
* @return
*/
public static String getPinYinInitialLetter(String chineseCharacter) {
StringBuilder res = new StringBuilder();
for (char cc : chineseCharacter.toCharArray()) {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(cc);
if (ArrayUtils.isNotEmpty(pinyinArray)) {
res.append(pinyinArray[0].charAt(0));
}
}
return res.toString();
}
}
测试类如下:
class PinYinUtilTest {
@Test
void getPingYin() {
System.out.println(PinYinUtil.getPingYin("你好"));
System.out.println(PinYinUtil.getPingYin("你好ccc,china"));
}
@Test
void getPinYinInitialLetter() {
System.out.println(PinYinUtil.getPinYinInitialLetter("你好ccc,china"));
}
}
以上是关于汉语拼音util的主要内容,如果未能解决你的问题,请参考以下文章
elasticsearch代码片段,及工具类SearchEsUtil.java
java.util.MissingResourceException: Can't find bundle for base name init, locale zh_CN问题的处理(代码片段