- maven依赖
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
- 工具类封装
package comg.yang.httpstest.controller.domain;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PinYinUtils {
/***
* ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
* ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
* ^[\u4E00-\u9FA5]+$ 匹配简体
*/
// private static final String REG_EXP = "^[\u4E00-\u9FFF]+$";
private static final Pattern PATTERN = Pattern.compile("^[\u4E00-\u9FFF]+$");
/**
* 汉字转拼音首写字母
*
* @param chinese
* @return
*/
public static String convertChinese2Capital(String chinese) {
return convertChinese2Pinyin(chinese, false);
}
/***
* 将汉字转成拼音(取首字母或全拼)
*
* @param hanzi
* @param flag 是否全拼
* @return
*/
public static String convertChinese2Pinyin(String hanzi, boolean flag) {
if (hanzi == null || "".equals(hanzi.trim())) {
return "";
}
StringBuilder sb = new StringBuilder();
char[] charArray = hanzi.toCharArray();
for (char unit : charArray) {
if (match(String.valueOf(unit))) { // 是汉字,则转拼音
String pinyin = convertSingleHanzi2Pinyin(unit);
if (flag) {
sb.append(pinyin);
} else {
sb.append(pinyin.charAt(0));
}
} else {
sb.append(unit);
}
}
return sb.toString();
}
/**
* 将单个汉字转成拼音
*
* @param hanzi
* @return
*/
private static String convertSingleHanzi2Pinyin(char hanzi) {
HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
String[] res;
StringBuilder sb = new StringBuilder();
try {
res = PinyinHelper.toHanyuPinyinStringArray(hanzi, outputFormat);
sb.append(res[0]); // 对于多音字,只用第一个拼音
} catch (Exception e) {
return "";
}
return sb.toString();
}
/***
* @param str 源字符串
* @return 是否匹配
*/
public static boolean match(String str) {
Matcher matcher = PATTERN.matcher(str);
return matcher.find();
}
public static void main(String[] args) {
System.err.println("全拼:" + PinYinUtils.convertChinese2Pinyin("大哥大嫂,过年好!", true));
System.err.println("首字母:" + PinYinUtils.convertChinese2Capital("大哥大嫂,过年好!"));
}
}
- 测试输出
全拼:dagedasao,guonianhao!
首字母:dgds,gnh!
标签:return,拼音,汉字,hanzi,static,sb,Java,String
From: https://www.cnblogs.com/JaxYoun/p/17460564.html