「Java 开发工具」3种方式实现汉字转拼音工具类(Chinese2Pinyin.java)

410 阅读3分钟

女18.jpg

文字处理:汉字转繁体、得到汉字拼音、拼音首字母 等

一、Maven 依赖

第一种依赖 Jar(pinyin4j)
<!-- 汉字转拼音jar包 -->
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
</dependency>
第二种依赖 Jar(jpinyin)

转载:文章引用-讲解

<dependency>
    <groupId>com.github.stuxuhai</groupId>
    <artifactId>jpinyin</artifactId>
    <version>1.1.8</version>
</dependency>
第三种依赖 Jar(bopomofo4j)

Bopomofo4j 讲解】。

<!-- 
	github地址:https://github.com/rnkrsoft/Bopomofo4j
	gitee地址:https://gitee.com/rnkrsoft/Bopomofo4j
 -->
<dependency>
    <groupId>com.rnkrsoft.bopomofo4j</groupId>
    <artifactId>bopomofo4j</artifactId>
    <version>1.0.0</version>
</dependency>

⚠ 注意:上面的依赖 并非是 下面程序的依赖!讲解

二、源码

(1)pinyin4j 使用示例

maven 引入

<!--汉字转拼音1:pinyin4j-->
<dependency>
    <groupId>com.belerweb</groupId>
    <artifactId>pinyin4j</artifactId>
    <version>2.5.1</version>
</dependency>

Java 代码示例

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;

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

/***
 * 1、中文转成全拼
 * 2、中文转成首字母
 * 3、中文转成ascii码
 *
 * @author Admin
 */
public class ChineseToEnglish {
    /**
     * 将汉字转换为全拼
     *
     * @param src
     * @return
     */
    public static String getPingYin(String src) {

        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else
                    t4 += java.lang.Character.toString(t1[i]);
            }
            // System.out.println(t4);
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }

    /**
     * 返回中文的首字母
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

    /**
     * 将字符串转移为ASCII码
     *
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }

    public static boolean isMobileNOA(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    public static void main(String[] args) {
//        System.out.println(getPingYin("广州科腾"));       //广州科腾 --> guangzhouketeng
//        System.out.println(getPingYin("广州科腾").substring(0,1));//获取到首字母
//        System.out.println(getPinYinHeadChar("綦江县"));  //綦江县   --> qjx
//        System.out.println(getCnASCII("綦江县"));         //綦江县   --> e7b6a6e6b19fe58ebf
//        ChineseToEnglish.getPinYinHeadChar("广州用能");//获取传入的中文的首字母字符串 例如 广州用能  --> gzyn
    }
}

(2)jpinyin 使用示例

① 引入依赖包

<!--汉字转拼音2:jpinyin-->
<dependency>
    <groupId>com.github.stuxuhai</groupId>
    <artifactId>jpinyin</artifactId>
    <version>1.1.8</version>
</dependency>

② Java 代码演示

public class PinYinUtil {

    public static void main(String[] args) throws PinyinException {
        String str = "你好世界";
        String p1 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_MARK);
        System.out.println(p1);// nǐ,hǎo,shì,jiè
        String p2 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITH_TONE_NUMBER);
        System.out.println(p2); // ni3,hao3,shi4,jie4
        String p3 = PinyinHelper.convertToPinyinString(str, ",", PinyinFormat.WITHOUT_TONE);
        System.out.println(p3); // ni,hao,shi,jie
        String shortPinyin = PinyinHelper.getShortPinyin(str);
        System.out.println(shortPinyin);// nhsj
    }

}
(3)bopomofo4j 使用实例

① maven 依赖引入

<!--汉字转拼音3:bopomofo4j-->
<dependency>
    <groupId>com.rnkrsoft.bopomofo4j</groupId>
    <artifactId>bopomofo4j</artifactId>
    <version>1.0.0</version>
</dependency>

② Java 代码示例

/**
 * 测试 bopomofo4j 依赖使用示例
 */
public static void testBopomofo4j() {
    //汉语句子->声母音调拼音
    String v1 = Bopomofo4j.pinyin("中国人!", 0, false, false, " ");
    System.out.println(v1);//控制台输出 zhōng guó rén!
    //汉语句子->数字音调拼音
    String v2 = Bopomofo4j.pinyin("患难与共的兄弟!!", 1, false, false, " ");
    System.out.println(v2);//控制台输出 huan0 nan0 yu3 gong0 de0 xiong0 di4!!
    //汉语句子->无音调拼音
    String v3 = Bopomofo4j.pinyin("this is a pinyin library!这是一个汉语拼音库!!", 2, false, false, " ");
    System.out.println(v3);//控制台输出 this is a pinyin library! zhe shi yi ge han yu pin yin ku!!
    //繁体->简体
    String v4 = Bopomofo4j.cht2chs("APM(Actions Per Minute)是一個在遊戲");
    System.out.println(v4);//APM(Actions Per Minute)是一个在游戏
    //简体->繁体
    String v5 = Bopomofo4j.chs2cht("APM(Actions Per Minute)是一个在游戏");
    System.out.println(v5);//APM(Actions Per Minute)是一個在遊戲
}

三、演示

pinyin4j 使用测试结果图示例

image.png


🙏至此,非常感谢阅读🙏

女18.jpg