Java 提取汉字的首字母、拼音

  • 代码
  • 2022-10-03 17:42:53

POM

<!--汉字提取-->
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.0</version>
</dependency>

Java代码

public class CharacterUtil {

    /**
     * 提取字符串字母
     *
     * @param str    字符串
     * @param isFull true:全拼 false:首字母
     * @return
     */
    public static String getStrLetter(String str, boolean isFull) {
        // 过滤掉特殊字符,只留汉字
        String reg = "[^\u4e00-\u9fa5]";
        str = str.replaceAll(reg, "");
        return convertStr2Pinyin(str, isFull).toUpperCase();
    }

    /**
     * 将汉字转成拼音
     *
     * @param str    字符串
     * @param isFull true:全拼 false:首字母
     * @return
     */
    private static String convertStr2Pinyin(String str, boolean isFull) {
        /***
         * ^[\u2E80-\u9FFF]+$ 匹配所有东亚区的语言
         * ^[\u4E00-\u9FFF]+$ 匹配简体和繁体
         * ^[\u4E00-\u9FA5]+$ 匹配简体
         */
        String regExp = "^[\\u4E00-\\u9FA5]+$";
        StringBuffer sb = new StringBuffer();
        if (str == null || "".equals(str.trim())) {
            return "";
        }
        String pinyin = "";
        for (int i = 0; i < str.length(); i++) {
            char unit = str.charAt(i);
            if (match(String.valueOf(unit), regExp)) {
                pinyin = convertSingleStr2Pinyin(unit);
                if (isFull) {
                    sb.append(pinyin);
                } else {
                    sb.append(pinyin.charAt(0));
                }
            } else {
                sb.append(unit);
            }
        }
        return sb.toString();
    }

    /**
     * 将单个汉字转成拼音
     *
     * @param str 字符
     * @return
     */
    private static String convertSingleStr2Pinyin(char str) {
        HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
        outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] res;
        StringBuffer sb = new StringBuffer();
        try {
            res = PinyinHelper.toHanyuPinyinStringArray(str, outputFormat);
            sb.append(res[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        return sb.toString();
    }

    private static boolean match(String str, String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.find();
    }

}

备注

忘记参考哪个大牛的文档了,若发现请告知,谢谢

本文地址:https://blog.csdn.net/qq_35571217/article/details/107694377

猜你喜欢