终结者:借助pinyin4j相关jar包将汉字转换为全拼的拼音

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;

public class Test {
	
    /**
     * 将汉字转换为全拼的拼音。
     * @param caseType 当为1时获取的首字母为小写,否则为大写。
     * @author 高焕杰
     */
    public static String getPinYinAllChar(String zn_str, int caseType) {
        char[] strChar = zn_str.toCharArray();
        HanyuPinyinOutputFormat hanYuPinOutputFormat = new HanyuPinyinOutputFormat();
        // 输出设置,大小写,音标方式等
        if(1 == caseType) {
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        } else {
            hanYuPinOutputFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        }
        hanYuPinOutputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        hanYuPinOutputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
       
        StringBuffer pyStringBuffer = new StringBuffer();
        String[] strString = new String[strChar.length];
        try {
            for (int i = 0; i < strChar.length; i++) {
                if (Character.toString(strChar[i]).matches("[\\u4E00-\\u9FA5]+")) {//如果是汉字字符
                	strString = PinyinHelper.toHanyuPinyinStringArray(strChar[i], hanYuPinOutputFormat);//将汉字的几种全拼都存到strString数组中
                    pyStringBuffer.append(strString[0]);//取出该汉字全拼的第一种读音并连接到字符串pyStringBuffer后
                } else {//如果不是汉字字符,直接取出字符并连接到字符串pyStringBuffer后
                	pyStringBuffer.append(Character.toString(strChar[i]));
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return pyStringBuffer.toString();
    }
    
	public static void main(String[] args) {
        String cnStr = "刘菲菲";
        System.out.println(getPinYinAllChar(cnStr,1));   //输出“liufeifei”
        System.out.println(getPinYinAllChar(cnStr,2));   //输出“LIUFEIFEI”
	}
}
【 0分下载相关jar包】

你可能感兴趣的:(终结者:借助pinyin4j相关jar包将汉字转换为全拼的拼音)