中文转首字母

中文转首字母工具类

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class PinyinUtil {
    
    // 简体中文的编码范围从B0A1(45217)至F7FE(63486)
    private static final int BEGIN = 45217;
    private static final int END = 63486;
    
    // '啊'是GB2312中的出现的第一个汉字,即"啊"是代表首字母a的第一个汉字
    // i,u,v都不做声母;自定规则:跟随前面的字母即可
    private static final char[] CHAR_TABLE = {'啊', '芭', '擦', '搭', '蛾', '发', '噶', '哈',
        '哈', '击', '喀', '垃', '妈', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌',
        '塌', '挖', '昔', '压', '匝',};
    
    // 对应首字母区间表
    private static final char[] INITIAL_TABLE = {'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',};
    
    // 二十六个字母区间对应二十七个端点,GB2312码汉字区间十进制表示
    private static final int[] TABLE = new int[27];
    
    // 数字
    private static final Set<String> NUMBERS = new HashSet<>(
        Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"));
    
    // 初始化
    static {
        for (int i = 0; i < 26; i++) {
            // 得到GB2312码的首字母区间端点表,十进制。
            TABLE[i] = gbValue(CHAR_TABLE[i]);
        }
        // 区间表结尾
        TABLE[26] = END;
    }
    
    public static String first(String s) {
        String[] chars = s.split("");
        StringBuilder sb = new StringBuilder(chars.length);
        for (String c : chars) {
            String l = getFirstLetter(c);
            sb.append(l);
        }
        return sb.toString();
    }
    
    /**
     * 。 根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串
     *
     * @param s -
     * @return -
     */
    private static String getFirstLetter(String s) {
        String result = "";
        if (s == null || s.isEmpty()) {
            return "#";
        }
        if (NUMBERS.contains(s)) {
            return s;
        }
        char cs = s.charAt(0);
        try {
            
            result += char2Initial(cs);
            
        } catch (Exception e) {
            result = "%";
            
        }
        return result;
    }
    
    /**
     * 输入字符,得到该字符的声母,英文字母返回对应的大写字母,其他非简体汉字返回 '#'
     *
     * @param ch -
     * @return -
     */
    private static char char2Initial(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 < BEGIN) || (gb > END)) {
            return '#';
        }
        int i;
        for (i = 0; i < 26; i++) {
            // 判断匹配码表区间,匹配到就break,判断区间形如“[,)”
            if ((gb >= TABLE[i]) && (gb < TABLE[i + 1])) {
                break;
            }
        }
        // 补上GB2312区间最右端
        if (gb == END) {
            i = 25;
        }
        // 在码表区间中,返回首字母
        return INITIAL_TABLE[i];
    }
    
    
    /**
     * 将一个汉字(GB2312)转换为十进制表示
     *
     * @param ch -
     * @return -
     */
    private static int gbValue(char ch) {
        String str = "";
        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) {
        System.out.println(first("你好"));
    }
}


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容