获取文字拼音

有时我们需要获取文字的拼音,根据首字母排序,如城市,姓名等,此处给出两种方法。第一种需要引入第三方jar包,但是兼容GBK和UTF-8的编码。第二种不用引包,但是只在GBK下生效。
我之前在新浪博客中也发过博文,具体可见地址
Java中获取中文首字母

这两种方法都是参考自网络,此处给出地址
地址1
地址2
地址3

以下为方法1源码
jar包为pinyin4j,请百度下载

public class PinYin {

    public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {

                if (java.lang.Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    t4 += java.lang.Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return t4;
    }

    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word,
                    new HanyuPinyinOutputFormat());
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert;
    }

    public static String getCnASCII(String cnStr) {
        StringBuffer strBuf = new StringBuffer();
        byte[] bGBK = cnStr.getBytes();
        for (int i = 0; i < bGBK.length; i++) {
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
        }
        return strBuf.toString();
    }

    public static void main(String[] args) {
        String cnStr ="我去年买了个表";
        System.out.println(getPingYin(cnStr));
        System.out.println(getPinYinHeadChar(cnStr));
    }

}

方法2的源码

“`
public class PinYin2 {
private static final int GB_SP_DIFF = 160;
private static final int[] secPosvalueList = { 1601, 1637, 1833, 2078,
2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730,
3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600 };

private static final char[] firstLetter = { 'a', 'b', 'c', 'd', 'e', 'f',
        'g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
        'w', 'x', 'y', 'z' };

public static String getFirstLetter(String oriStr) {
    String str = oriStr.toLowerCase();
    StringBuffer buffer = new StringBuffer();
    char ch;
    char[] temp;
    for (int i = 0; i < str.length(); i++) { 
        ch = str.charAt(i);
        temp = new char[] { ch };
        byte[] uniCode = new String(temp).getBytes();
        if (uniCode[0] < 128 && uniCode[0] > 0) { 
            buffer.append(temp);
        } else {
            buffer.append(convert(uniCode));
        }
    }
    return buffer.toString();
}

private static char convert(byte[] bytes) {

    char result = '-';
    int secPosvalue = 0;
    int i;
    for (i = 0; i < bytes.length; i++) {
        bytes[i] -= GB_SP_DIFF;
    }
    secPosvalue = bytes[0] * 100 + bytes[1];
    for (i = 0; i < 23; i++) {
        if (secPosvalue >= secPosvalueList[i]
                && secPosvalue < secPosvalueList[i + 1]) {
            result = firstLetter[i];
            break;
        }
    }
    return result;
}

public static void main(String args[]) {
    System.out.println(PinYin2.getFirstLetter("我去年买了个表"));
}```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值