public static String substring(String value, int beginIndex, int length) {
String chinese = "[\u0391-\uFFE5]";
if (length > value.length()) {
throw new IllegalArgumentException(
"length must less than value.length()");
}
char[] charArray = value.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = beginIndex,twice=0; twice < length; i++,twice++) {
/* 获取一个字符 */
String temp = String.valueOf(charArray[i]);
/* 如果是中文多增加一个字符 */
if (temp.matches(chinese)) {
if(length-twice>1){
++twice;
sb.append(temp);
}
} else {
sb.append(temp);
}
}
return sb.toString();
}
写了个截取中文字符串的方法
最新推荐文章于 2022-03-25 14:35:08 发布