欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取首字母工具方法

程序员文章站 2022-05-26 14:22:44
...

获取首字母工具方法:

public static void main(String[] args) {
        String s = getFirstPinyin("刘");
        String s1 = getFirstPinyin("贺");
        String s2 = getFirstPinyin("曹");
        String s3 = getFirstPinyin("百");
        String s4 = getFirstPinyin("钟");
        String s5 = getFirstPinyin("曼");
        String s6 = getFirstPinyin("额");
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
        System.out.println(s5);
        System.out.println(s6);
    }

    public static String getFirstPinyin(String chs) {

        int nCode = 0;
        byte nHigh, nLow;
        String key, value;
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < chs.length(); i++) {
            key = chs.substring(i, i + 1);
            try {
                value = "";
                byte[] buff = key.getBytes("gb2312");
                if (buff.length >= 2) {
                    nHigh = (buff[0]);
                    nLow = (buff[1]);
                    nCode = (nHigh + 256 - 0XA0) * 100 + nLow + 256 - 0XA0;
                    if (nCode > 1600 && nCode < 5590) {
                        for (int j = 22; j >= 0; j--) {
                            if (nCode >= li_SecPosValue[j]) {
                                value = lc_FirstLetter[j];
                                break;
                            }
                        }
                    } else {
                        nCode = (nHigh + 256 - 0XA0 - 56) * 94 + nLow + 256 - 0XA0 - 1;
                        if (nCode >= 0 && nCode < TABLE_LEN) {
                            value = ls_SecondSecTable.substring(nCode, nCode + 1);
                        } else {
                            value = String.valueOf(nHigh) + String.valueOf(nLow);
                        }
                    }
                } else {
                    value = key;
                }
                buffer.append(value);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return buffer.toString();
    }

    private static final int TABLE_LEN = 3008;

    private static String ls_SecondSecTable = "CJWGNSPGCGNE[Y[BTYYZDXYKYGT[JNNJQMBSGZSCYJSYY[PGKBZGY[YWJKGKLJYWKPJQHY[W[DZLSGMRYPYWWCCKZNKYYGTTNJJNYKKZYTCJNMCYLQLYPYQFQRPZSLWBTGK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DDYKZDDHQHSDZSCTARLLTKZLGECLLKJLQJAQNBDKKGHPJTZQKSECSHALQFMMGJNLYJBBTMLYZXDCJPLDLPCQDHZYCBZSCZBZMSLJFLKRZJSNFRGJHXPDHYJYBZGDLQCSEZGXLBLGYXTWMABCHECMWYJYZLLJJYHLG[DJLSLYGKDZPZXJ"
            + "YYZLWCXSZFGWYYDLYHCLJSCMBJHBLYZLYCBLYDPDQYSXQZBYTDKYXJY[CNRJMPDJGKLCLJBCTBJDDBBLBLCZQRPPXJCJLZCSHLTOLJNMDDDLNGKAQHQHJGYKHEZNMSHRP[QQJCHGMFPRXHJGDYCHGHLYRZQLCYQJNZSQTKQJYMSZSWLCFQQQXYFGGYPTQWLMCRNFKKFSYYLQBMQAMMMYXCTPSHCPTXXZZSMPHPSHMCLMLDQFYQXSZYYDYJZZHQPDSZGLSTJBCKBXYQZJSGPSXQZQZRQTBDKYXZK"
            + "HHGFLBCSMDLDGDZDBLZYYCXNNCSYBZBFGLZZXSWMSCCMQNJQSBDQSJTXXMBLTXZCLZSHZCXRQJGJYLXZFJPHYMZQQYDFQJJLZZNZJCDGZYGCTXMZYSCTLKPHTXHTLBJXJLXSCDQXCBBTJFQZFSLTJBTKQBXXJJLJCHCZDBZJDCZJDCPRNPQCJPFCZLCLZXZDMXMPHJSGZGSZZQLYLWTJPFSYASMCJBTZKYCWMYTCSJJLJCQLWZMALBXYFBPNLSFHTGJWEJJXXGLLJSTGSHJQLZFKCGNNNSZFDEQ"
            + "FHBSAQTGYLBXMMYGSZLDYDQMJJRGBJTKGDHGKBLQKBDMBYLXWCXYTTYBKMRTJZXQJBHLMHMJJZMQASLDCYXYQDLQCAFYWYXQHZ";

    private static int li_SecPosValue[] = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472,
            3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249 };

    private static String lc_FirstLetter[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O",
            "P", "Q", "R", "S", "T", "W", "X", "Y", "Z" };
相关标签: 工具方法