Java獲取漢字拼音或拼音首字母

          Posted on 2012-04-18 21:58 myfavorite 閱讀(8487) 評論(8)  編輯  收藏
          在進行模糊查詢時,經常用到使用漢字拼音或者首字母進行匹配查詢。以下是獲取漢字拼音或者首字母的java代碼實現
          package com;

          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 GetPinyin {

              /**
               * 得到 全拼
               * 
               * 
          @param src
               * 
          @return
               
          */
              public static String getPingYin(String src) {
                  char[] t1 = null;
                  t1 = src.toCharArray();
                  String[] t2 = new String[t1.length];
                  HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
                  t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
                  t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
                  t3.setVCharType(HanyuPinyinVCharType.WITH_V);
                  String t4 = "";
                  int t0 = t1.length;
                  try {
                      for (int i = 0; i < t0; i++) {
                          // 判斷是否為漢字字符
                          if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
                              t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                              t4 += t2[0];
                          } else {
                              t4 += java.lang.Character.toString(t1[i]);
                          }
                      }
                      return t4;
                  } catch (BadHanyuPinyinOutputFormatCombination e1) {
                      e1.printStackTrace();
                  }
                  return t4;
              }

              /**
               * 得到中文首字母
               * 
               * 
          @param str
               * 
          @return
               
          */
              public static String getPinYinHeadChar(String str) {

                  String convert = "";
                  for (int j = 0; j < str.length(); j++) {
                      char word = str.charAt(j);
                      String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
                      if (pinyinArray != null) {
                          convert += pinyinArray[0].charAt(0);
                      } else {
                          convert += word;
                      }
                  }
                  return convert;
              }

              /**
               * 將字符串轉移為ASCII碼
               * 
               * 
          @param cnStr
               * 
          @return
               
          */
              public static String getCnASCII(String cnStr) {
                  StringBuffer strBuf = new StringBuffer();
                  byte[] bGBK = cnStr.getBytes();
                  for (int i = 0; i < bGBK.length; i++) {
                      // System.out.println(Integer.toHexString(bGBK[i]&0xff));
                      strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
                  }
                  return strBuf.toString();
              }

              public static void main(String[] args) {

                  String cnStr = "戩浜";
                  System.out.println(getPingYin(cnStr));
                  System.out.println(getPinYinHeadChar(cnStr));
              }

          }
          pinyin4j-2.5.0.jar

          Feedback

          # re: Java獲取漢字拼音或拼音首字母[未登錄]  回復  更多評論   

          2013-07-16 14:57 by 張三
          張三

          # re: Java獲取漢字拼音或拼音首字母[未登錄]  回復  更多評論   

          2013-09-14 18:00 by aaa
          網上這么多就你這個靠譜了,,謝謝~~~

          # re: Java獲取漢字拼音或拼音首字母[未登錄]  回復  更多評論   

          2013-11-21 16:20 by aaa
          網上這么多,就你最靠譜了

          # re: Java獲取漢字拼音或拼音首字母[未登錄]  回復  更多評論   

          2014-01-14 09:53 by 張三
          等等等

          # re: Java獲取漢字拼音或拼音首字母[未登錄]  回復  更多評論   

          2014-01-14 14:44 by 匿名
          非常感謝

          # re: Java獲取漢字拼音或拼音首字母[未登錄]  回復  更多評論   

          2014-01-23 08:55 by 匿名
          不支持多音字

          # re: Java獲取漢字拼音或拼音首字母  回復  更多評論   

          2014-01-23 09:30 by myfavorite
          @匿名
          多音字這個還真不太好處理。你有什么好的方法?

          # re: Java獲取漢字拼音或拼音首字母  回復  更多評論   

          2014-03-20 23:07 by luo080428
          找了一晚上,就樓主的可以,在GBK還是UTF-8的編碼下都行,網上其他的方式只是在GBK編碼下可以。樓主好人,好人一生平安

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 湘潭县| 宜昌市| 高陵县| 吉水县| 宜良县| 来凤县| 澄城县| 涟源市| 洱源县| 青海省| 瑞金市| 丹巴县| 图木舒克市| 花莲市| 乌拉特前旗| 吉林省| 高淳县| 缙云县| 五常市| 霸州市| 滦南县| 渭南市| 柘荣县| 墨竹工卡县| 桂林市| 农安县| 老河口市| 邮箱| 喀喇| 五华县| 福安市| 祥云县| 榆社县| 宜宾县| 拜城县| 芷江| 砚山县| 健康| 万安县| 邛崃市| 西畴县|