void

          取中文字符的拼音首字母

          /**
          * 取中文字符的拼音首字母
          * @author
          */
          public class GB2Alpha {
          /**
          * 字母Z使用了兩個標(biāo)簽,這里有27個值,i, u, v都不做聲母, 跟隨前面的字母
          */
              private char[] chartable =
                      {
                          '啊', '芭', '擦', '搭', '蛾', '發(fā)', '噶', '哈', '哈',
                          '擊', '喀', '垃', '媽', '拿', '哦', '啪', '期', '然',
                          '撒', '塌', '塌', '塌', '挖', '昔', '壓', '匝', '座'
                      };

              private char[] alphatable =
                      {
                          'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',

                          'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
                      };


              private int[] table = new int[27];

              /**
              * 初始化
              */
              {
                  for (int i = 0; i < 27; ++i) {
                      table = gbValue(chartable);
                  }
              }

              public GB2Alpha() {

              }

              /**
              * 主函數(shù),輸入字符,得到他的聲母,英文字母返回對應(yīng)的大寫字母,其他非簡體漢字返回 '0'
              */
              public char Char2Alpha(char ch) {

                  if (ch >= 'a' && ch <= 'z')
                      return (char) (ch - 'a' + 'A');
                  if (ch >= 'A' && ch <= 'Z')
                      return ch;


                  int gb = gbValue(ch);
                  if (gb < table[0])
                      return '0';


                  int i;
                  for (i = 0; i < 26; ++i) {
                      if (match(i, gb))
                          break;
                  }

                  if (i >= 26)
                      return '0';
                  else
                      return alphatable;
              }

              /**
              * 根據(jù)一個包含漢字的字符串返回一個漢字拼音首字母的字符串
              * @param SourceStr
              * @return
              */
              public String String2Alpha(String SourceStr) {
                  String Result = "";
                  int StrLength = SourceStr.length();
                  int i;
                  try {
                      for (i = 0; i < StrLength; i++) {
                          Result += Char2Alpha(SourceStr.charAt(i));
                      }
                  } catch (Exception e) {
                      Result = "";
                  }
                  return Result;
              }

              private boolean match(int i, int gb) {
                  if (gb < table)
                      return false;

                  int j = i + 1;

                  //字母Z使用了兩個標(biāo)簽
                  while (j < 26 && (table == table))
                      ++j;

                  if (j == 26)
                      return gb <= table;
                  else
                      return gb < table;

              }

              /**
              * 取出漢字的編碼
              */
              private int gbValue(char ch) {
                  String str = new String();
                  str += ch;
                  try {
                      byte[] bytes = str.getBytes("GB2312");
                      if (bytes.length < 2)
                          return 0;
                      return (bytes[0] << 8 & 0xff00) + (bytes[1] &
                              0xff);
                  } catch (Exception e) {
                      return 0;
                  }

              }


              public static void main(String[] args) {
                  GB2Alpha obj1 = new GB2Alpha();
                  System.out.println(obj1.String2Alpha("小華"));
                  return;
              }
          }

          posted on 2010-07-29 16:44 void 閱讀(275) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 青州市| 泰安市| 榆中县| 句容市| 卢湾区| 巴彦淖尔市| 韶山市| 斗六市| 礼泉县| 广西| 宁蒗| 大理市| 广安市| 荣昌县| 横山县| 马山县| 松江区| 北流市| 彰武县| 西乡县| 莆田市| 华阴市| 六枝特区| 安福县| 常州市| 临夏市| 台前县| 昆山市| 万源市| 巨鹿县| 北流市| 旬邑县| 博湖县| 甘南县| 肇东市| 惠东县| 陵川县| 安图县| 锡林浩特市| 黔西县| 宣汉县|