需要引入的jar
- <dependency>
- <groupId>com.belerwebgroupId>
- <artifactId>pinyin4jartifactId>
- <version>2.5.0version>
- dependency>
实现工具类
- package com.julong.util;
-
- import java.util.regex.Pattern;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- 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 julong
- * @date 2023年11月22日 上午11:20:30
- * @desc
- */
- public class PinyinUtils {
-
- //日志
- private static final Logger logger = LoggerFactory.getLogger(PinyinUtils.class);
-
- /**
- * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
- * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
- * ^[\u4E00-\u9FA5]+$ 匹配简体
- */
- public final static String REGEX="^[\u4E00-\u9FFF]+$";
- //设置格式
- public static HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();;
-
- public PinyinUtils(){
-
- }
-
- /**
- * 汉子转拼音
- * @param data 汉子转拼音
- * @return han zi huan pin yin
- * @author julong
- * @date 2023年11月22日 上午11:21:50
- * @desc
- */
- public static String convertPinyin(String data){
- logger.debug("【汉子转拼音工具】-转换拼音输入参数-data:{}",data);
- if(StringUtils.isBlank(data)){
- return "";
- }
- outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
- outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
- String newData = "";
- //转为 char
- char[] chars = data.toCharArray();
- try {
- //判断是否为汉子
- for (char c : chars) {
- if(Pattern.matches(REGEX,String.valueOf(c))){
- //转码
- String[] a = PinyinHelper.toHanyuPinyinStringArray(c,outputFormat);
- newData = newData + a[0] +" ";
- }else{
- newData = newData + c + " ";
- }
- }
- } catch (BadHanyuPinyinOutputFormatCombination e) {
- // TODO: handle exception
- logger.error("【汉子转拼音工具】-转换拼音出现问题:{}",e);
- return data;
- }
- return newData;
- }
-
- /**
- * 汉子转拼音
- * @param data 汉子转拼音
- * @param caseType
HanyuPinyinCaseType - * @return
小写:han zi huan pin yin
大写:HAN ZI HUAN PIN YIN
- * @author julong
- * @date 2023年11月22日 上午11:25:30
- * @desc
- */
- public static String convertPinyin(String data,HanyuPinyinCaseType caseType){
- logger.debug("【汉子转拼音工具】-转换拼音输入参数-data:{},caseType:{}",data,caseType.getName());
- if(StringUtils.isBlank(data)){
- return "";
- }
- outputFormat.setCaseType(caseType);
- outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
- outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
- String newData = "";
- //转为 char
- char[] chars = data.toCharArray();
- try {
- //判断是否为汉子
- for (char c : chars) {
- if(Pattern.matches(REGEX,String.valueOf(c))){
- //转码
- String[] a = PinyinHelper.toHanyuPinyinStringArray(c,outputFormat);
- newData = newData + a[0] +" ";
- }else{
- String str = String.valueOf(c);
- if(caseType.getName().equals(HanyuPinyinCaseType.UPPERCASE.getName())){
- str = str.toUpperCase();
- }
- newData = newData + str + " ";
- }
- }
-
- } catch (BadHanyuPinyinOutputFormatCombination e) {
- // TODO: handle exception
- logger.error("【汉子转拼音工具】-转换拼音出现问题:{}",e);
- return data;
- }
- return newData;
- }
-
- /**
- * 汉子转拼音首字母
- * @param data
- * @return
- * @author julong
- * @date 2023年11月22日 上午11:30:28
- * @desc
- */
- public static String convertPinyinInitials(String data){
- logger.debug("【汉子转拼音工具】-转换拼音输入参数-data:{}",data);
- if(StringUtils.isBlank(data)){
- return "";
- }
- String newData = "";
- //转为 char
- char[] chars = data.toCharArray();
- //判断是否为汉子
- for (char c : chars) {
- if(Pattern.matches(REGEX,String.valueOf(c))){
- //转码
- String[] a = PinyinHelper.toHanyuPinyinStringArray(c);
- newData = newData + a[0].charAt(0);
- }else{
- //不是汉子
- newData = newData + c;
- }
- }
- return newData;
- }
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String zhongwen = "中文转拼音,pin yin";
- System.out.println(convertPinyinInitials(zhongwen).toUpperCase());
- System.out.println(convertPinyin(zhongwen));
- System.out.println(convertPinyin(zhongwen,HanyuPinyinCaseType.LOWERCASE));
- System.out.println(convertPinyin(zhongwen,HanyuPinyinCaseType.UPPERCASE));
- }
-
- }