使用PinYin4J汉字转拼音

  1 import net.sourceforge.pinyin4j.PinyinHelper;
  2 import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
  3 import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
  4 import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
  5 import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
  6 
  7 /**
  8  * 汉字转拼音,英文字符不变
  9  * @version 1.0
 10  * 
 11  */
 12 public class PinYinToolkit {
 13     
 14     /**
 15      * 汉字转换汉语拼音首字母,英文字符不变
 16      * 例如:北京--> bj
 17      * @param chinese 汉字
 18      * @return String
 19      */
 20     public static String cn2FirstSpell(String chinese){
 21         StringBuilder pinyin = new StringBuilder();
 22         char[] nameChar = chinese.toCharArray();
 23         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 24         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 25         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 26         for(int i = 0; i < nameChar.length;i++){
 27             if(nameChar[i] > 128){
 28                 try {
 29                     String[] py = PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat);
 30                     if(py != null){
 31                         pinyin.append(py[0].charAt(0));
 32                     }
 33                 } catch (BadHanyuPinyinOutputFormatCombination e) {
 34                     e.printStackTrace();
 35                 }
 36             } else {
 37                 pinyin.append(nameChar[i]);
 38             }
 39         }
 40         return pinyin.toString();
 41     }
 42     
 43     /**
 44      * 汉字转换汉语拼音,英文字符不变
 45      * 例如:北京--> beijing
 46      * @param chinese 汉字
 47      * @return String
 48      */
 49     public static String cn2Spell(String chinese){
 50         StringBuilder pinyin = new StringBuilder();
 51         char[] nameChar = chinese.toCharArray();
 52         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 53         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 54         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 55         for(int i = 0; i < nameChar.length;i++){
 56             if(nameChar[i] > 128){
 57                 try {
 58                     String[] py = PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat);
 59                     if(py != null){
 60                         pinyin.append(py[0]);
 61                     }
 62                 } catch (BadHanyuPinyinOutputFormatCombination e) {
 63                     e.printStackTrace();
 64                 }
 65             } else {
 66                 pinyin.append(nameChar[i]);
 67             }
 68         }
 69         return pinyin.toString();
 70     }
 71     
 72     /**
 73      * 拼音转换汉语拼音,拼音首字母大写,英文字符不变
 74      * 例如:北京--> BeiJing
 75      * @param chinese 汉字
 76      * @return String
 77      */
 78     public static String cn2SpellFirstUpperCase(String chinese){
 79         StringBuilder pinyin = new StringBuilder();
 80         char[] nameChar = chinese.toCharArray();
 81         HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
 82         defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
 83         defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
 84         for(int i = 0; i < nameChar.length;i++){
 85             if(nameChar[i] > 128){
 86                 try {
 87                     String[] py = PinyinHelper.toHanyuPinyinStringArray(nameChar[i],defaultFormat);
 88                     if(py != null){                
 89                         pinyin.append(py[0].substring(0, 1).toUpperCase() + py[0].substring(1));
 90                     }
 91                 } catch (BadHanyuPinyinOutputFormatCombination e) {
 92                     e.printStackTrace();
 93                 }
 94             } else {
 95                 pinyin.append(nameChar[i]);
 96             }
 97         }
 98         return pinyin.toString();
 99     }
100 }

 

posted on 2013-01-28 22:57  DongLiYang  阅读(519)  评论(0编辑  收藏  举报

导航