[Java]Pinyin4j中文转化为拼音工具类使用(精炼写法)

2021-03-12 1303点热度 0人点赞 0条评论

前言

我们在做项目的时候,可能会遇到将中文转化成拼音来做排序的依据,不过如果没有工具类,要把全部的中文都对应好拼音,也是一个不小的工作量,好在有现成的类库已经帮我们准备好了,我们只要花很小的时间,稍加定制就可以了,那就是pinyin4j

更方便的是,连常用的将字符转成首字母拼音,或者全拼,这些常用的,都用工具类写好了!

引入pinyin4j依赖

如果是Maven,加入pom.xml:

<!-- https://mvnrepository.com/artifact/com.belerweb/pinyin4j -->
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
</dependency>

如果是Gradle,加入build.gradle

implementation("com.belerweb:pinyin4j:2.5.1")

工具类,外加测试

如何使用都写在注释里了,请取用

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;

public class Pinyin4jUtils {

    public static void main(String[] args) {
        // 中文转成全拼大写测试,并用空白分割打印
        String[] strings = getFullPinyin("拼音测试", true);
        // 使用String.join来输出,如果你用的JDK比较落后,那么就迭代每个string,拼接出来
        System.out.println(String.join(" ", strings)); // PIN YIN CE SHI

        // 中文转成首字母小写测试
        String[] strings2 = getInitialPinyin("拼音测试", false);
        System.out.println(String.join("", strings2)); // pycs
    }

    /**
     * 将字符串转成首字母拼音
     * @param source 字符
     * @param uppercase 是否大写
     */
    public static String[] getInitialPinyin(String source, boolean uppercase) {
        char[] chars = source.toCharArray();
        String[] strings = new String[chars.length];
        for (int i = 0; i < chars.length; i++) {
            strings[i] = getInitialPinyin(chars[i], uppercase);
        }
        return strings;
    }

    /**
     * 将char转成首字母拼音
     * @param source 字符
     * @param uppercase 是否大写
     */
    public static String getInitialPinyin(char source, boolean uppercase) {
        String result = getFullPinyin(source, uppercase);
        if (result == null || result.length() == 0) {
            return "";
        }
        return result.substring(0, 1);
    }

    /**
     * 将字符串转成全部拼音
     * @param source 字符
     * @param uppercase 是否大写
     */
    public static String[] getFullPinyin(String source, boolean uppercase) {
        char[] chars = source.toCharArray();
        String[] strings = new String[chars.length];
        for (int i = 0; i < chars.length; i++) {
            strings[i] = getFullPinyin(chars[i], uppercase);
        }
        return strings;
    }

    /**
     * 将char转成全部拼音
     * @param source 字符
     * @param uppercase 是否大写
     */
    public static String getFullPinyin(char source, boolean uppercase) {
        if (source <= 128) {
            return String.valueOf(source);
        }

        if (String.valueOf(source).matches("[\\u4E00-\\u9FA5]+")) {//中文
            HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
            // 输出设置,大小写,音标方式等
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

            String[] strings = null;
            try {
                strings = PinyinHelper.toHanyuPinyinStringArray(source, hanYuPinOutputFormat);
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                return "";
            }
            if (strings == null || strings.length == 0) {
                return "";
            }
            return uppercase ? strings[0].toUpperCase() : strings[0];
        } else if (((int) source >= 65 && (int) source <= 90)
                || ((int) source >= 97 && (int) source <= 122)) {//英文
            return String.valueOf(source);
        } else {
            return "";
        }

    }
}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论