java漢字轉拼音,取漢字首字母,支持繁體

原文地址:http://noobjava.iteye.com/blog/855811

Java代碼 
  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.HanyuPinyinVCharType;  
  6. import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;  
  7.   
  8. public class Pinyin {  
  9.     /** 
  10.      * 將漢字轉換爲全拼 
  11.      *  
  12.      * @param src 
  13.      * @return String 
  14.      */  
  15.     public static String getPinYin(String src) {  
  16.         char[] t1 = null;  
  17.         t1 = src.toCharArray();  
  18.         // System.out.println(t1.length);  
  19.         String[] t2 = new String[t1.length];  
  20.         // System.out.println(t2.length);  
  21.         // 設置漢字拼音輸出的格式  
  22.         HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
  23.         t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
  24.         t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
  25.         t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
  26.         String t4 = "";  
  27.         int t0 = t1.length;  
  28.         try {  
  29.             for (int i = 0; i < t0; i++) {  
  30.                 // 判斷是否爲漢字字符  
  31.                 // System.out.println(t1[i]);  
  32.                 if (Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {  
  33.                     t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);// 將漢字的幾種全拼都存到t2數組中  
  34.                     t4 += t2[0];// 取出該漢字全拼的第一種讀音並連接到字符串t4後  
  35.                 } else {  
  36.                     // 如果不是漢字字符,直接取出字符並連接到字符串t4後  
  37.                     t4 += Character.toString(t1[i]);  
  38.                 }  
  39.             }  
  40.         } catch (BadHanyuPinyinOutputFormatCombination e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  
  44.         return t4;  
  45.     }  
  46.   
  47.     /** 
  48.      * 提取每個漢字的首字母 
  49.      *  
  50.      * @param str 
  51.      * @return String 
  52.      */  
  53.     public static String getPinYinHeadChar(String str) {  
  54.         String convert = "";  
  55.         for (int j = 0; j < str.length(); j++) {  
  56.             char word = str.charAt(j);  
  57.             // 提取漢字的首字母  
  58.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
  59.             if (pinyinArray != null) {  
  60.                 convert += pinyinArray[0].charAt(0);  
  61.             } else {  
  62.                 convert += word;  
  63.             }  
  64.         }  
  65.         return convert;  
  66.     }  
  67.   
  68.     /** 
  69.      * 將字符串轉換成ASCII碼 
  70.      *  
  71.      * @param cnStr 
  72.      * @return String 
  73.      */  
  74.     public static String getCnASCII(String cnStr) {  
  75.         StringBuffer strBuf = new StringBuffer();  
  76.         // 將字符串轉換成字節序列  
  77.         byte[] bGBK = cnStr.getBytes();  
  78.         for (int i = 0; i < bGBK.length; i++) {  
  79.             // System.out.println(Integer.toHexString(bGBK[i] & 0xff));  
  80.             // 將每個字符轉換成ASCII碼  
  81.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff)+" ");  
  82.         }  
  83.         return strBuf.toString();  
  84.     }  
  85.   
  86.     public static void main(String[] args) {  
  87.         String cnStr = "中華人民共和國";  
  88.         System.out.println(getPinYin(cnStr));  
  89.         System.out.println(getPinYinHeadChar(cnStr));  
  90. //      System.out.println(getCnASCII(cnStr));  
  91.     }  
  92.   
  93. }  
 

發佈了110 篇原創文章 · 獲贊 16 · 訪問量 41萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章