zoukankan      html  css  js  c++  java
  • 获取中文的完整拼音并输出

    1.添加maven依赖

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

    2.获取拼音逻辑

    public static String converterToFirstSpell(String chines) {
            String pinyinName = "";
            char[] nameChar = chines.toCharArray();
            HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
            defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
            defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            for (int i = 0; i < nameChar.length; i++) {
                String s = String.valueOf(nameChar[i]);
                if (s.matches("[\u4e00-\u9fa5]")) {
                    try {
                        String[] mPinyinArray = PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat);
                        pinyinName += mPinyinArray[0];
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                } else {
                    pinyinName += nameChar[i];
                }
            }
            return pinyinName;
        }
  • 相关阅读:
    Mysql知识:事务
    Mysql知识 索引补充
    Mysql知识:索引
    Leetcode 78 子集
    每日总结
    个人冲刺-第二阶段(8)
    每日总结
    个人冲刺-第二阶段(7)
    每日总结
    个人冲刺-第二阶段(6)
  • 原文地址:https://www.cnblogs.com/haoyangblog/p/7575363.html
Copyright © 2011-2022 走看看