汉字转换为拼音Java工具类

本文整理自网络,介绍了Java实现汉字转拼音的方法。首先需在pom.xml中引入依赖,接着给出工具类,还进行了使用测试。若有不当之处欢迎指正,文章已被收录进《程序员成长笔记(第四部)》。

声明:本文是对https://www.cnblogs.com/DreamDrive/p/5762078.html的整理,感谢博主分享。


我就是爱音乐~


准备工作:在pom.xml中引入依赖。

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

工具类:

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;

/**
 * 汉字转换为拼音 工具类
 *
 * 声明:整理优化自https://www.cnblogs.com/DreamDrive/p/5762078.html
 *
 * @author JustryDeng
 * @date 2019/3/18 9:49
 */
public class ChineseToPinyinUtil {

    /** (一个)中文 正则表达式 */
    private static final String CHINESE_REGEX = "[\u4e00-\u9fa5]";

    /**
     * 将指定字符串中的中文转为汉语拼音,其余非中文的部分不变
     * 注:拼音小写
     *
     * @param originChinese
     *            要转成拼音的中文
     * @param abbreviation
     *            是否简写     true,   【张三】转换为【zs】
     *                        false,  【张三】转换为【zhangsan】
     * @param caseType
     *            控制拼音大小写
     *
     * @return 拼音
     * @throws BadHanyuPinyinOutputFormatCombination
     *             HanyuPinyinOutputFormat格式有误
     */
    public static String convertChineseToPinyin(String originChinese,
                                                boolean abbreviation,
                                                HanyuPinyinCaseType caseType)
                                                throws BadHanyuPinyinOutputFormatCombination {
        // 将字符串 转换为 字符数组
        char[] chineseCharArray = originChinese.trim().toCharArray();
        // 设置转换格式
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 输出拼音全部小写(默认即为小写)
        defaultFormat.setCaseType(caseType);
        // 不带声调
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        /*
         * 含ü的字有 【女】【吕】【略】【虐】等
         *
         * WITH_V            设置  【ü】 转换为 【v】 ,           如:【女】 转换后为 【nv】
         * WITH_U_AND_COLON  设置  【ü】 转换为 【u:】,           如:【女】 转换后为 【nu:】
         * WITH_U_UNICODE    设置  【ü】 转换为 【ü】,即:原输出,   如:【女】 转换后为 【nü】
         */
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuilder hanYuPinYinResult = new StringBuilder(6);
        boolean isChinese;
        String tempStr;
        for (char cStr : chineseCharArray) {
            isChinese = String.valueOf(cStr).matches(CHINESE_REGEX);
            // 如果字符是中文,则将中文转为汉语拼音
            if (isChinese) {
                tempStr = PinyinHelper.toHanyuPinyinStringArray(cStr, defaultFormat)[0];
                tempStr = abbreviation ? tempStr.substring(0, 1) : tempStr;
                hanYuPinYinResult.append(tempStr);
            } else {
                // 如果字符不是中文,则不转换
                hanYuPinYinResult.append(cStr);
            }
        }
        return hanYuPinYinResult.toString();
    }

}

使用测试:

控制台输出:

 

^_^ 如有不当之处,欢迎指正

^_^ 整理自以下链接 
               https://www.cnblogs.com/DreamDrive/p/5762078.html

^_^ 本文已经被收录进《程序员成长笔记(第四部)》,笔者JustryDeng

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值