• 汉字转拼音


    一,pinyin4j 工具包

    汉字转拼音我们需要使用到pinyin4j这个工具jar包,它可以处理中文转换成拼音(汉语拼音,罗马拼音等)

    1,常用类介绍

    PinyinHelper 提供了汉字转拼音的主要方法

    HanyuPinyinOutputFormat 定义如何输出拼音,

    HanyuPinyinCaseType 提供了拼音输出的样式

    • LOWERCASE:输出小写,
    • UPPERCASE:输出大写

    HanyuPinyinToneType 输出音标的设置

    • WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常),
    • WITH_TONE_NUMBER:1-4数字表示音标,
    • WITHOUT_TONE:没有音标

    HanyuPinyinVCharType 特殊音标ü的设置(了解下)

    • WITH_V:用v表示ü,
    • WITH_U_AND_COLON:用"u:"表示ü,
    • WITH_U_UNICODE:直接用ü

    二,使用pinyin4j

    1,引用jar包

     >
         >com.belerweb>
         >pinyin4j>
         >2.5.0>
    >
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2,写一个工具类

    工具类中的两个方法可以合在一起

    /**
     * @description:汉字转拼音工具类
     */
    public class HanZiToPinYinUtil {
    
        public static void main(String[] args) {
            String allPinYin = getAllPinYin("我是中国人00");
            String firstPinYin = getFirstPinYin("我是中国人00");
            String allPinYin2 = getAllPinYin("王宇");
            System.out.println(allPinYin);
            System.out.println(allPinYin2);
            System.out.println(firstPinYin);
        }
    
        /**
         * 获取汉字字符串的全拼音
         * @param hanZi '我是中国人00'
         * @return
         */
        public static String getAllPinYin(String hanZi){
            //这个类定义了如何输出汉语拼音。
            HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
            pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//小写形式输出,LOWERCASE:输出小写,UPPERCASE:输出大写
            pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//输出音标设置,WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常),WITH_TONE_NUMBER:1-4数字表示音标,WITHOUT_TONE:没有音标
            pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);//特殊音标ü的设置,(WITH_V:用v表示ü,WITH_U_AND_COLON:用"u:"表示ü,WITH_U_UNICODE:直接用ü)
    
            char[] hanziChars = hanZi.trim().toCharArray();//汉字数组,去掉前后多余的空格
            StringBuilder stringBuilder = new StringBuilder();
            try {
                for (int i = 0; i < hanziChars.length; i++) {
                    String hanziString = Character.toString(hanziChars[i]);//得到单个汉字,例如‘中’
                    if(hanziString.matches("[\\u4e00-\\u9fa5]")){//判断是否是汉字
                        String[] pinYinAll = PinyinHelper.toHanyuPinyinStringArray(hanziChars[i],pinyinOutputFormat);//得到此汉字的所有读音,例如中是一个多音字,其结果就是['zhōng','zhòng']
                        String oneHanZiPinYin = pinYinAll[0];//直接取一个拼音为汉字的拼音,例如zhōng
                        stringBuilder.append(oneHanZiPinYin);
                    }else {//不是汉字原样输出
                        stringBuilder.append(hanziString);
                    }
    
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            String allPinYin = stringBuilder.toString();//返回中文字符串的全拼,例如:wŏshìzhōngzhòngguórén00
            return allPinYin;
        }
    
        /**
         * 获取汉字字符串的首字母拼音
         * @param hanZi '我是中国人00'
         * @return
         */
        public static String getFirstPinYin(String hanZi){
            //这个类定义了如何输出汉语拼音。
            HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
            pinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);//小写形式输出,LOWERCASE:输出小写,UPPERCASE:输出大写
            pinyinOutputFormat.setToneType(HanyuPinyinToneType.WITH_TONE_MARK);//输出音标设置,WITH_TONE_MARK:直接用音标符(必须设置WITH_U_UNICODE,否则会抛出异常),WITH_TONE_NUMBER:1-4数字表示音标,WITHOUT_TONE:没有音标
            pinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);//特殊音标ü的设置,(WITH_V:用v表示ü,WITH_U_AND_COLON:用"u:"表示ü,WITH_U_UNICODE:直接用ü)
    
            char[] hanziChars = hanZi.trim().toCharArray();//去掉前后多余的空格,转为汉字数组
            StringBuilder stringBuilder = new StringBuilder();
            try {
                for (int i = 0; i < hanziChars.length; i++) {
                    String hanziString = Character.toString(hanziChars[i]);//得到单个汉字,例如‘中’
                    if(hanziString.matches("[\\u4e00-\\u9fa5]")){//判断是否是汉字
                        String[] pinYinAll = PinyinHelper.toHanyuPinyinStringArray(hanziChars[i],pinyinOutputFormat);//得到此汉字的所有读音,例如中是一个多音字,其结果就是['zhōng','zhòng']
                        String firstString = Character.toString(pinYinAll[0].charAt(0));//去第一个拼音的第一个字符为汉字首字母
                        stringBuilder.append(firstString);
                    }else {//不是汉字原样输出
                        stringBuilder.append(hanziString);
                    }
                }
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            String allPinYin = stringBuilder.toString();//返回中文字符串的首字母拼音
            return allPinYin;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    输出结果

    wŏshìzhōngguórén00
    wángyŭ
    wszgr00
    
    • 1
    • 2
    • 3
  • 相关阅读:
    推特大裁员后,马斯克与白宫发生冲突!META 大批裁员正在路上
    分享一下怎么在微信小程序上卖东西
    Python小游戏——外星人入侵(保姆级教程)第一章 07调整飞船速度 08限制飞船活动范围
    CLion 配置 Qt 开发环境
    爬虫破解:解决CSRF-Token反爬问题 - 上海市发展和改革委员会
    Leetcode139. 单词拆分
    基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)
    Linux——基本指令
    HCIA网络课程第四周作业
    网站Github资源收集 ,此篇没有找到github地址,作者整理了自己在Github中的starred项目可以直接在此网站进行访问。
  • 原文地址:https://blog.csdn.net/ybsgsg/article/details/126137412