Java 汉子转拼音

1. 引入: pinyin4j-1.1.0 包;  http://pan.baidu.com/s/1eQ8a874

2. 转换;

private static String ChineseToPinyin(String name){

        String pinyinName="";

        char[] nameChar = name.toCharArray(); 

        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();

        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);

        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        for (int i = 0; i < nameChar.length; i++) {

            char singlechar=nameChar[i];

            if (nameChar[i] > 128) {

                try {
            //这里可以取全拼,首字母等各种组合 pinyinName
+= PinyinHelper.toHanyuPinyinStringArray(singlechar, defaultFormat)[0];
}
catch (Exception e) { e.printStackTrace(); } }else{ pinyinName +=singlechar; } } return pinyinName.toLowerCase(); }

3. 将结果保存进入数据库;

搞定

你可能感兴趣的:(java)