码迷,mamicode.com
首页 > 其他好文 > 详细

汉语拼音util

时间:2018-11-03 12:35:06      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:one   添加   16px   str   pattern   class   .so   note   system   

使用pinyin4j-2.5.1.jar.

maven依赖为

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

代码如下:
package com.zad.JDK8.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 org.apache.commons.lang3.ArrayUtils;

import java.util.regex.Pattern;

/**
 * 描述:
 * 汉字转韩语拼音util
 *
 * @author zad
 * @create 2018-11-01 12:25
 */
public class PinYinUtil {
    /**
     * 汉语拼音format
     */
    private static final HanyuPinyinOutputFormat hanyuPinyinOutputFormat = new HanyuPinyinOutputFormat();

    /**
     * 汉字对应正则表达式 pattern
     */
    private static final Pattern chineseCharacterPattern = Pattern.compile("[\\u4E00-\\u9FA5]");

    //字母对应正则
    private static final Pattern characterPattern = Pattern.compile("[a-zA-Z]");

    static {
        hanyuPinyinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        hanyuPinyinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        hanyuPinyinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
    }

    private PinYinUtil() {
        throw new AssertionError("Util禁止反射实例化");
    }

    /**
     * 将chineseCharacter中汉字转为拼音,其余所有字符直接略过,
     * 若需要额外处理,则需要额外判断
     *
     * @param chineseCharacter
     * @return
     */
    public static String getPingYin(String chineseCharacter) {
        StringBuilder sb = new StringBuilder();
        try {
            for (char cc : chineseCharacter.toCharArray()) {
                if (chineseCharacterPattern.matcher(String.valueOf(cc)).matches()) {
                    sb.append(PinyinHelper.toHanyuPinyinStringArray(cc, hanyuPinyinOutputFormat)[0]);
                }
               /* // 如果是字母,直接添加
                if(characterPattern.matcher(String.valueOf(cc)).matches())){
                    sb.append(cc);
                }*/
            }
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return sb.toString();
    }

    /**
     * 将chineseCharacter中汉字转为拼音并获取首字母,其余所有字符直接略过,
     *
     * @param chineseCharacter
     * @return
     */
    public static String getPinYinInitialLetter(String chineseCharacter) {
        StringBuilder res = new StringBuilder();
        for (char cc : chineseCharacter.toCharArray()) {
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(cc);
            if (ArrayUtils.isNotEmpty(pinyinArray)) {
                res.append(pinyinArray[0].charAt(0));
            }
        }
        return res.toString();
    }
}

 

 

测试类如下:

class PinYinUtilTest {

    @Test
    void getPingYin() {
        System.out.println(PinYinUtil.getPingYin("你好"));
        System.out.println(PinYinUtil.getPingYin("你好ccc,china"));
    }

    @Test
    void getPinYinInitialLetter() {
        System.out.println(PinYinUtil.getPinYinInitialLetter("你好ccc,china"));
    }

}

 

汉语拼音util

标签:one   添加   16px   str   pattern   class   .so   note   system   

原文地址:https://www.cnblogs.com/zad27/p/9900007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!