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