汉字转为拼音的工具类

1:引入依赖


    com.belerweb
    pinyin4j
    2.5.0

2:工具类

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

/***
 * 汉字工具类
 * @author wantao
 * @since 2018.10.26
 *
 */
public class ChineseCharacterUtil {
    /**
     * @param hanzi * @param full
     * @Author zhangBin
     * @Description //TODO 将汉字转成拼音(取首字母或全拼)
     * @Date 15:53 2019/7/19
     **/
    public static String convertHanzi2Pinyin(String hanzi, boolean full) {
        /***
         * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
         * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
         * ^[\u4E00-\u9FA5]+$ 匹配简体
         */
        String regExp = "^[\u4E00-\u9FFF]+$";
        StringBuffer sb = new StringBuffer();
        if (hanzi == null || "".equals(hanzi.trim())) {
            return "";
        }
        String pinyin = "";
        for (int i = 0; i < hanzi.length(); i++) {
            char unit = hanzi.charAt(i);
            if (match(String.valueOf(unit), regExp))//是汉字,则转拼音
            {
                pinyin = convertSingleHanzi2Pinyin(unit);
                if (full) {
                    sb.append(pinyin);
                } else {
                    sb.append(pinyin.charAt(0));
                }
            } else {
                sb.append(unit);
            }
        }
        return sb.toString();
    }

    /**
     * @param hanzi
     * @Author zhangBin
     * @Description //TODO 将单个汉字转成拼音
     * @Date 15:54 2019/7/19
     **/
    private static String convertSingleHanzi2Pinyin(char hanzi) {
        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] res;
        StringBuffer sb = new StringBuffer();
        try {
            res = PinyinHelper.toHanyuPinyinStringArray(hanzi, outputFormat);
            sb.append(res[0]);//对于多音字,只用第一个拼音
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return sb.toString();
    }

    /***
     * @param str 源字符串
     * @param regex 正则表达式
     * @return 是否匹配
     */
    public static boolean match(String str, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }

    /**
     * @param str
     * @Author zhangBin
     * @Description //TODO 获取汉字字符串的首字母,并且大写
     * @Date 15:52 2019/7/19
     **/
    public static String getInitials(String str) {
        String convertHanzi2Pinyin = convertHanzi2Pinyin(str, true);
        String regex = ".*[a-zA-z].*";
        for (int i = 0; i < convertHanzi2Pinyin.length(); i++) {
            char c = convertHanzi2Pinyin.charAt(i);
            String value = String.valueOf(c);
            if (value.matches(regex)) {
                return value.toUpperCase();
            }
        }
        return null;
    }
}

你可能感兴趣的:(汉字转换为拼音工具类)