pinyin4j使用崩溃

PinYinUtils 使用过程中出现崩溃,数组越界问题处理

compile 'com.belerweb:pinyin4j:2.5.1'    gradle依赖
public static String getPingYin(String inputString) {
        if (TextUtils.isEmpty(inputString)) {
            return "#";
        }
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        char[] input = inputString.trim().toCharArray();
        StringBuilder output = new StringBuilder();
        try {
            for (char curchar : input) {
                if (Character.toString(curchar).matches("[\\u4E00-\\u9FA5]+")) {
                    String[] temp = PinyinHelper.toHanyuPinyinStringArray(curchar, format);
                    if (temp == null || temp.length == 0) {
                        // unicode_to_hanyu_pinyin.txt里写死了部分汉字没有拼音,比如\u8985, 覅,
                        // 也就是正常情况这个temp可能为空,
                        Log.e(TAG, "获取拼音失败:" + inputString + " -> " + curchar);
                        output.append("#");
                    } else {
                        output.append(temp[0]);
                    }
                } else if (Character.isAlphabetic(curchar)) {
                    output.append(Character.toLowerCase(curchar));
                } else {
                    output.append("#");
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            e.printStackTrace();
        }
        return output.toString();
    }

    private static boolean isChineseCharacter(char ch) {
        return '\u4E00' < ch && ch < '\u9FA5';
    }

https://blog.csdn.net/haovip123/article/details/50994187?utm_source=blogxgwz3
https://www.cnblogs.com/scetopcsa/p/4724308.html
https://www.cnblogs.com/bluestorm/archive/2012/07/23/2605412.html
https://www.cnblogs.com/liaojie970/p/5490180.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值