将汉语转换为拼音

用到了一个拼音类库 pinyin4j-2.5.0.jar


核心代码如下:


/**
 * 汉字转拼音
 *
 * @param src
 * @return String
 */
public static String getPinYin(String src) {
    char[] t1 = null;
    t1 = src.toCharArray();
    // System.out.println(t1.length);
    String[] t2 = new String[t1.length];
    // System.out.println(t2.length);
    // 汉字转拼音
    HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
    t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
    t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
    t3.setVCharType(HanyuPinyinVCharType.WITH_V);
    String t4 = "";
    int t0 = t1.length;
    try {
        for (int i = 0; i < t0; i++) {
            if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);//
                if(t2 != null){
                    t4 += t2[0];//
                }
            } else {
                t4 += Character.toString(t1[i]);
            }
        }
    } catch (BadHanyuPinyinOutputFormatCombination e) {
        LoggerHanlder.handle("汉字转拼音异常", e);
    }
    return t4;
}


你可能感兴趣的:(java,库,生成拼音)