1. 程式人生 > >工具類——將中文名轉換成大寫拼音

工具類——將中文名轉換成大寫拼音

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 SpellHelper {
    //將中文轉換為英文
    public static String getEname(String name)
     {
        HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
         pyFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);


        try {
return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
return name;
    }


    public static String getUpEname(String name) {
        char[] strs = name.toCharArray();
        String newname = "";
        for(int i = 0; i < strs.length; i++){
        newname += getEname(strs[i]+" ");
        }
        return newname;
    }


    public static void main(String[] args) {
        System.out.println(getUpEname("寒洛"));
    }


}