Java 获取汉字首字母

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值