利用pingyin4j 将汉字转换为拼音

jar包:http://pan.baidu.com/s/11ikCY

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class pinyin {
    
    public static void main(String[] args) {
        System.out.println(new pinyin().Console('你'));
    }
    
    public String Console(char order) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        String[] pinyinArray = null;
        try {
            pinyinArray = PinyinHelper.toHanyuPinyinStringArray(order, format);
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        
        if (pinyinArray!=null) {
            return pinyinArray[0];
        } else {
            return order + "";
        }
    }
}

点赞