汉字拼音缩写输出工具类

pom文件

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

工具类

/**
     * 提取每个汉字的首字母
     * 
     * @param str
     *            汉子输入
     * @return String 拼音缩写输出
     */
    public static String getPinYinHeadChar(String str) {
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            // 提取汉字的首字母
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

 

posted @ 2018-08-10 15:45  发疯的man  阅读(778)  评论(0编辑  收藏  举报