JAVA获取中文名字的首字母

http://blog.csdn.net/leeyefang/article/details/9082255

1、调用FirstLetterUtil类的getFirstLetter()方法,获取姓名的首字母。如:“阿鲁卓玛”获取首字母是“alzm”。

String firstLetter = FirstLetterUtil.getFirstLetter(“阿鲁卓玛”);

2、代码,如下:

[java]  view plain copy
  1. public class FirstLetterUtil {  
  2.     private static int BEGIN = 45217;  
  3.     private static int END = 63486;  
  4.     // 按照声母表示,这个表是在GB2312中的出现的第一个汉字,也就是说“啊”是代表首字母a的第一个汉字。  
  5.     // i, u, v都不做声母, 自定规则跟随前面的字母  
  6.     private static char[] chartable = {'啊''芭''擦''搭''蛾''发''噶''哈',  
  7.         '哈''击''喀''垃''妈''拿''哦''啪''期''然''撒''塌''塌',  
  8.         '塌''挖''昔''压''匝',};  
  9.     // 二十六个字母区间对应二十七个端点  
  10.     // GB2312码汉字区间十进制表示  
  11.     private static int[] table = new int[27];  
  12.     // 对应首字母区间表  
  13.     private static char[] initialtable = {'a''b''c''d''e''f''g',  
  14.         'h''h''j''k''l''m''n''o''p''q''r''s''t',  
  15.         't''t''w''x''y''z',};  
  16.     // 初始化  
  17.     static {  
  18.         for (int i = 0; i < 26; i++) {  
  19.             table[i] = gbValue(chartable[i]);// 得到GB2312码的首字母区间端点表,十进制。  
  20.         }  
  21.         table[26] = END;// 区间表结尾  
  22.     }  
  23.     /** 
  24.      * 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串 最重要的一个方法,思路如下:一个个字符读入、判断、输出 
  25.      */  
  26.     public static String getFirstLetter(String sourceStr) {  
  27.         String result = "";  
  28.         String str = sourceStr.toLowerCase();  
  29.         int StrLength = str.length();  
  30.         int i;  
  31.         try {  
  32.             for (i = 0; i < StrLength; i++) {  
  33.                 result += Char2Initial(str.charAt(i));  
  34.             }  
  35.         } catch (Exception e) {  
  36.             result = "";  
  37.         }  
  38.         return result;  
  39.     }  
  40.     /** 
  41.      * 输入字符,得到他的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '0' 
  42.      */  
  43.     private static char Char2Initial(char ch) {  
  44.         // 对英文字母的处理:小写字母转换为大写,大写的直接返回  
  45.         if (ch >= 'a' && ch <= 'z') {  
  46.             return ch;  
  47.         }  
  48.         if (ch >= 'A' && ch <= 'Z') {  
  49.             return ch;  
  50.         }  
  51.         // 对非英文字母的处理:转化为首字母,然后判断是否在码表范围内,  
  52.         // 若不是,则直接返回。  
  53.         // 若是,则在码表内的进行判断。  
  54.         int gb = gbValue(ch);// 汉字转换首字母  
  55.         if ((gb < BEGIN) || (gb > END))// 在码表区间之前,直接返回  
  56.         {  
  57.             return ch;  
  58.         }  
  59.         int i;  
  60.         for (i = 0; i < 26; i++) {// 判断匹配码表区间,匹配到就break,判断区间形如“[,)”  
  61.             if ((gb >= table[i]) && (gb < table[i + 1])) {  
  62.                 break;  
  63.             }  
  64.         }  
  65.         if (gb == END) {//补上GB2312区间最右端  
  66.             i = 25;  
  67.         }  
  68.         return initialtable[i]; // 在码表区间中,返回首字母  
  69.     }  
  70.     /** 
  71.      * 取出汉字的编码 cn 汉字 
  72.      */  
  73.     private static int gbValue(char ch) {// 将一个汉字(GB2312)转换为十进制表示。  
  74.         String str = new String();  
  75.         str += ch;  
  76.         try {  
  77.             byte[] bytes = str.getBytes("GB2312");  
  78.             if (bytes.length < 2) {  
  79.                 return 0;  
  80.             }  
  81.             return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);  
  82.         } catch (Exception e) {  
  83.             return 0;  
  84.         }  
  85.     }  
  86. }  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值