Java汉字转拼音(解决方案)

Posted 剑桥大表哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java汉字转拼音(解决方案)相关的知识,希望对你有一定的参考价值。

思考一下日常开发中如何将 博主最帅 转为 bozhuzuishuai 或是 bzzs 等等。解析字符串?转码?这些方法都过时了。下面博主给大家推荐一个方法。让你三步完成转换。

步骤一

  • 导入maven依赖
        <dependency>
            <groupId>com.belerweb</groupId>
            <artifactId>pinyin4j</artifactId>
            <version>2.5.0</version>
            <scope>compile</scope>
        </dependency>

步骤二

  • 编写工具类以及转换方法
package com.example.jinzproduct.util;

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.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author mengzhichao
 * @create 2022-11-10-10:00
 */
public class ChangeChinesePinyin 
    public static Map<String, String> changeChinesePinyin(String chinese) throws BadHanyuPinyinOutputFormatCombination 
        Map<String, String> pinyin = new HashMap<String, String>();

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        StringBuffer fullPinyin = new StringBuffer();
        StringBuffer simplePinyin = new StringBuffer();
        StringBuffer firstPinyin = new StringBuffer();

        char[] chineseChar = chinese.toCharArray();
        for (int i = 0; i < chineseChar.length; i++) 
            String[] str = null;
            try 
                str = PinyinHelper.toHanyuPinyinStringArray(chineseChar[i],
                        format);
             catch (BadHanyuPinyinOutputFormatCombination e) 
                e.printStackTrace();
            
            if (str != null) 
                if (str.length == 0) 
                    continue;
                
                fullPinyin = fullPinyin.append(str[0].toString());
                simplePinyin = simplePinyin.append(str[0].charAt(0));

            
            if (str == null) 
                String regex = "^[0-9]*[a-zA-Z]*+$";
                Pattern pattern = Pattern.compile(regex);
                Matcher m = pattern.matcher(String.valueOf(chineseChar[i]));
                if (m.find()) 
                    fullPinyin = fullPinyin.append(chineseChar[i]);
                    simplePinyin = simplePinyin.append(chineseChar[i]);
                
            
        
        String[] name = PinyinHelper.toHanyuPinyinStringArray(chineseChar[0], format);
        if(name == null && fullPinyin.substring(0,1).matches("[a-zA-Z]"))
            name = new String[]fullPinyin.substring(0,1);
        else if (name == null)
            name = new String[]"#";
        
        firstPinyin = firstPinyin.append(name[0].charAt(0));
        pinyin.put("fullPinyin", fullPinyin.toString());
        pinyin.put("simplePinyin", simplePinyin.toString().toUpperCase());
        pinyin.put("groupPinyin", firstPinyin.toString().toUpperCase());
        return pinyin;
    

步骤三

  • 测试看结果
    public static void main(String[] args) throws BadHanyuPinyinOutputFormatCombination 
        Map<String, String> fydmcPinYinMap = changeChinesePinyin("博主最帅");

        System.out.println("首字母:"+fydmcPinYinMap.get("groupPinyin"));
        System.out.println("拼音全称:"+fydmcPinYinMap.get("fullPinyin"));
        System.out.println("拼音缩写:"+fydmcPinYinMap.get("simplePinyin"));
    

以上是关于Java汉字转拼音(解决方案)的主要内容,如果未能解决你的问题,请参考以下文章

Java汉字转拼音(解决方案)

Java汉字转拼音pinyin4j用法

java怎么根据汉字获取字的拼音首字母

汉字转拼音的Java类库——JPinyin

java 汉字转拼音

JAVA汉字转拼音