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

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;
    }
}

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

猜你喜欢

转载自blog.csdn.net/xmt1369758466/article/details/107410824