zoukankan      html  css  js  c++  java
  • java获取中问名字的首字母

     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     }  
  • 相关阅读:
    python 正则表达式 (重点) re模块
    python 异常处理
    面向对象 反射 特殊方法
    3. 容器备份与迁移
    2. Docker部署tomcat, nginx, redis,及docker私有仓库
    1. Docker快速入门(仓库,镜像,容器)
    Linux部署项目 shell脚本启动 及 Centos7开放指定端口
    nginx快速入门
    linux安装mysql8(完整图文笔记)
    Redis主从架构核心原理
  • 原文地址:https://www.cnblogs.com/javaweb2/p/6243677.html
Copyright © 2011-2022 走看看