博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java获取中文拼音首字母工具类
阅读量:4707 次
发布时间:2019-06-10

本文共 2611 字,大约阅读时间需要 8 分钟。

package com.sw.documentary.common.utils;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[i] = gbValue(chartable[i]);        }    }    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[i];    }    //根据一个包含汉字的字符串返回一个汉字拼音首字母的字符串    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[i])            return false;        int j = i + 1;        //字母Z使用了两个标签        while (j < 26 && (table[j] == table[i])) ++j;        if (j == 26)            return gb <= table[j];        else            return gb < table[j];    }    //取出汉字的编码    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("我爱你"));        System.out.println(obj1.String2Alpha("吴广"));        return;    }}

转载于:https://www.cnblogs.com/yht520/archive/2013/01/23/3589650.html

你可能感兴趣的文章
Python学习笔记——参数axis=0,1,2...
查看>>
【原创】Talend ETL Job日志框架——基于P&G项目的一些思考和优化
查看>>
深入了解sql语句以及应用实例
查看>>
如何使用ShareSDK实现Cocos2d-x的Android/iOS分享与授权
查看>>
求欧拉函数
查看>>
2018/01/01Java基础学习——如何通过dos系统的javadoc命令生成API文档
查看>>
LaTex公式在线转图片
查看>>
hadoop学习2----HDFS操作
查看>>
单元测试小结
查看>>
python 中的split()函数和os.path.split()函数
查看>>
搜索引擎的检索模型-查询与文档的相关度计算
查看>>
实现简单的对拍
查看>>
IOS MVC与MVVM的区别
查看>>
学习python之路_入门篇A
查看>>
向数据库中全部表中增加一个字段的SQL
查看>>
RapidMiner Studio之Process源码分析
查看>>
CF 5 A. Chat Server's Outgoing Traffic
查看>>
10.4 使用布局管理器3(CardLayout)
查看>>
uva 11143
查看>>
uva 1377
查看>>