关于汉字转拼音,看这一篇就够了。

1、依赖包的引入

Pinyin4j是一个流行的Java库,支持中文字符和拼音之间的转换,可以自主的控制转换拼音的格式,在我们日常开发的项目中,经常会遇到汉字转拼音,或者拼音搜索的场景,这时候使用Pinyin4j即可快速解决这个需求。
maven下的依赖:

        <dependency>
                <groupId>com.belerweb</groupId>
                <artifactId>pinyin4j</artifactId>
                <version>2.5.1</version>
        </dependency>

2、拼音格式化

Pinyin4j采用HanyuPinyinOutputFormat这个对象来设置拼音的格式。

2.1 大小写转化

outputFormat.setCaseType(HanyuPinyinCaseType);

HanyuPinyinCaseType.LOWERCASE 拼音小写方式输出

HanyuPinyinCaseType.UPPERCASE 拼音大写方式输出

2.2拼音声调格式转化

outputFormat.setToneType(HanyuPinyinToneType);

方法参数HanyuPinyinToneType有以下常量对象:

HanyuPinyinToneType.WITH_TONE_NUMBER 用数字表示声调

HanyuPinyinToneType.WITHOUT_TONE 无声调表示

HanyuPinyinToneType.WITH_TONE_MARK 用声调符号表示

2.3特殊拼音ü的显示格式

outputFormat.setVCharType(HanyuPinyinVCharType);

方法参数HanyuPinyinVCharType有以下常量对象:

HanyuPinyinVCharType.WITH_U_AND_COLON 以u:表示

HanyuPinyinVCharType.WITH_V 以v表示

HanyuPinyinVCharType.WITH_U_UNICODE 以ü表示

3.汉字转拼音

废话不多说,直接上代码

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;

/**
 * @ClassName ChineseToAlphabetic 
 * @Description TODO
 * @Author zzb
 * @Date 2020/7/17 10:23
 */
public class ChineseToAlphabetic {
    public static void main(String[] args) {
        ChineseToAlphabetic chineseToAlphabetic = new ChineseToAlphabetic();
        String str = "中文字符转拼音";
        System.out.println(str);
        System.out.println(chineseToAlphabetic.getAlphabetic(str));
        String str1 = "绿色";
        System.out.println(str1);
        System.out.println(chineseToAlphabetic.getAlphabetic(str1));
    }

    public  String getAlphabetic(String str) {
        //声明字符数组
        char[] alphabeticArray = null;
        //接收字符串中的单个字符
        alphabeticArray = str.toCharArray();
        //声明字符串数组用于接收单个汉字的拼音
        String[] singleChinese = new String[alphabeticArray.length];
        /*
        声明拼音格式对象,用于设置拼音格式
         */
        HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();
        hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        String result = " ";
        try{
            //遍历每个字符
            for (int i = 0; i < alphabeticArray.length ; i++){
                //判断当前字符是否为汉字
                if (java.lang.Character.toString(alphabeticArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                    singleChinese = PinyinHelper.toHanyuPinyinStringArray(alphabeticArray[i],hanyuPinyinOutputFormat);
                    result += singleChinese[0]+" ";
                }else{
                    result += " " + java.lang.Character.toString(alphabeticArray[i]);
                }
            }
        }catch (BadHanyuPinyinOutputFormatCombination e){
            e.printStackTrace();
        }
        return  result;
    }
}

输出结果如下:
结果输出图片
如有补充,欢迎评论~

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值