【Java】中文汉字转拼音(pinyin4j)

Maven导入依赖:

		
			com.belerweb
			pinyin4j
			2.5.0
		

编写中文汉字转拼音工具类:

package com.yealink.phonebook.utils;

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;

/**
 * Created by yl1794 on 2018/5/16.
 */
public class PinyinUtils {

    /**
     * 将字符串中的中文转化为拼音全拼,英文字符不变
     *
     * @param chines 汉字
     * @return 拼音
     */
    public static String getPingYinAll(String chines) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        StringBuilder output = new StringBuilder();
        if (chines != null && chines.length() > 0
                && !"null".equals(chines)) {
            char[] input = chines.trim().toCharArray();
            try {
                for (char anInput : input) {
                    if (Character.toString(anInput).matches(
                            "[\\u4E00-\\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(
                                anInput, format);
                        output.append(temp[0]);
                    } else
                        output.append(Character.toString(anInput));
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            return "*";
        }
        return output.toString();
    }

    /**
     * 将字符串中的中文转化为拼音首字母(大写),英文字符不变
     *
     * @param chines 汉字
     * @return 拼音
     */
    public static String getPingYinAllUpper(String chines) {
        StringBuilder pinyinName = new StringBuilder();
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char aNameChar : nameChar) {
            if (aNameChar > 128) {
                try {
                    pinyinName.append(PinyinHelper.toHanyuPinyinStringArray(
                            aNameChar, defaultFormat)[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName.append(aNameChar);
            }
        }
        return pinyinName.toString();
    }

    /**
     * 将字符串中的中文转化为拼音首字母(小写),英文字符不变
     *
     * @param chines 汉字
     * @return 拼音
     */
    public static String getPingYinAllLower(String chines) {
        StringBuilder pinyinName = new StringBuilder();
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char aNameChar : nameChar) {
            if (aNameChar > 128) {
                try {
                    pinyinName.append(PinyinHelper.toHanyuPinyinStringArray(
                            aNameChar, defaultFormat)[0].charAt(0));
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            } else {
                pinyinName.append(aNameChar);
            }
        }
        return pinyinName.toString();
    }
}

编写测试类:

package com.yealink.phonebook;

import com.yealink.phonebook.utils.PinyinUtils;

/**
 * Created by yl1794 on 2018/5/16.
 */
public class testPinYin {
    public static void main(String[] args){
        String test = "IT 娃哈哈 What 服务器工程师 shi";
        System.out.println(PinyinUtils.getPingYinAll(test));
        System.out.println(PinyinUtils.getPingYinAllUpper(test));
        System.out.println(PinyinUtils.getPingYinAllLower(test));
    }
}

测试结果如下:

IT wahaha What fuwuqigongchengshi shi
IT WHH What FWQGCS shi
IT whh What fwqgcs shi

Process finished with exit code 0

你可能感兴趣的:(Java)