pinyin4j工具类(转)

转载:http://liuzidong.iteye.com/blog/787582
pinyin4j的主页:http://pinyin4j.sourceforge.net/

pinyin4j能够根据中文字符获取其对应的拼音,而且拼音的格式可以定制。

pinyin4j是一个支持将中文转换到拼音的Java开源类库。

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 Pinyin4jUtil {   
  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.             e.printStackTrace();   
  42.         }   
  43.         return t4;   
  44.     }   
  45.   
  46.     /**  
  47.      * 提取每个汉字的首字母  
  48.      *   
  49.      * @param str  
  50.      * @return String  
  51.      */  
  52.     public static String getPinYinHeadChar(String str) {   
  53.         String convert = "";   
  54.         for (int j = 0; j < str.length(); j++) {   
  55.             char word = str.charAt(j);   
  56.             // 提取汉字的首字母   
  57.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);   
  58.             if (pinyinArray != null) {   
  59.                 convert += pinyinArray[0].charAt(0);   
  60.             } else {   
  61.                 convert += word;   
  62.             }   
  63.         }   
  64.         return convert;   
  65.     }   
  66.   
  67.     /**  
  68.      * 将字符串转换成ASCII码  
  69.      *   
  70.      * @param cnStr  
  71.      * @return String  
  72.      */  
  73.     public static String getCnASCII(String cnStr) {   
  74.         StringBuffer strBuf = new StringBuffer();   
  75.         // 将字符串转换成字节序列   
  76.         byte[] bGBK = cnStr.getBytes();   
  77.         for (int i = 0; i < bGBK.length; i++) {   
  78.             // System.out.println(Integer.toHexString(bGBK[i] & 0xff));   
  79.             // 将每个字符转换成ASCII码   
  80.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));   
  81.         }   
  82.         return strBuf.toString();   
  83.     }   
  84.   
  85.     public static void main(String[] args) {   
  86.         String cnStr = "中国";   
  87.         System.out.println(getPinYin(cnStr));   
  88.         System.out.println(getPinYinHeadChar(cnStr));   
  89.         System.out.println(getCnASCII(cnStr));   
  90.     }   
  91. }  
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 Pinyin4jUtil {  
    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.             e.printStackTrace();  
    42.         }  
    43.         return t4;  
    44.     }  
    45.   
    46.     /** 
    47.      * 提取每个汉字的首字母 
    48.      *  
    49.      * @param str 
    50.      * @return String 
    51.      */  
    52.     public static String getPinYinHeadChar(String str) {  
    53.         String convert = "";  
    54.         for (int j = 0; j < str.length(); j++) {  
    55.             char word = str.charAt(j);  
    56.             // 提取汉字的首字母  
    57.             String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
    58.             if (pinyinArray != null) {  
    59.                 convert += pinyinArray[0].charAt(0);  
    60.             } else {  
    61.                 convert += word;  
    62.             }  
    63.         }  
    64.         return convert;  
    65.     }  
    66.   
    67.     /** 
    68.      * 将字符串转换成ASCII码 
    69.      *  
    70.      * @param cnStr 
    71.      * @return String 
    72.      */  
    73.     public static String getCnASCII(String cnStr) {  
    74.         StringBuffer strBuf = new StringBuffer();  
    75.         // 将字符串转换成字节序列  
    76.         byte[] bGBK = cnStr.getBytes();  
    77.         for (int i = 0; i < bGBK.length; i++) {  
    78.             // System.out.println(Integer.toHexString(bGBK[i] & 0xff));  
    79.             // 将每个字符转换成ASCII码  
    80.             strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
    81.         }  
    82.         return strBuf.toString();  
    83.     }  
    84.   
    85.     public static void main(String[] args) {  
    86.         String cnStr = "中国";  
    87.         System.out.println(getPinYin(cnStr));  
    88.         System.out.println(getPinYinHeadChar(cnStr));  
    89.         System.out.println(getCnASCII(cnStr));  
    90.     }  

posted @ 2012-12-02 21:27  万法自然~  阅读(273)  评论(0)    收藏  举报