void

          取中文字符的拼音首字母

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

              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() {

              }

              /**
              * 主函數,輸入字符,得到他的聲母,英文字母返回對應的大寫字母,其他非簡體漢字返回 '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;
              }

              /**
              * 根據一個包含漢字的字符串返回一個漢字拼音首字母的字符串
              * @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使用了兩個標簽
                  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 閱讀(273) 評論(0)  編輯  收藏 所屬分類: Java

          主站蜘蛛池模板: 仁怀市| 漳浦县| 罗平县| 玛纳斯县| 虞城县| 和平区| 印江| 浦东新区| 贵阳市| 陆河县| 喜德县| 许昌县| 翁源县| 汝阳县| 文登市| 盘山县| 论坛| 西畴县| 鲁甸县| 铜山县| 南漳县| 南皮县| 沅陵县| 娄烦县| 平阴县| 延长县| 军事| 平邑县| 沙坪坝区| 九龙城区| 阿巴嘎旗| 永丰县| 南溪县| 保靖县| 镇远县| 绵阳市| 焦作市| 汉沽区| 聂拉木县| 西平县| 白山市|