public String String_length(String value, int maxSize) {
if(StringUtils.isNotBlank(value)){
int valueLength = 0;
String chinese = "[\u4e00-\u9fa5]";
for (int i = 0; i < value.length(); i++) {
String temp = value.substring(i, i + 1);
if (temp.matches(chinese)) {
valueLength += 2;
} else {
valueLength += 1;
}
if(valueLength == maxSize){
value = value.substring(0, i + 1);
break;
}
if(valueLength > maxSize){
if(temp.matches(chinese)){
value = value.substring(0, i);
break;
}else{
value = value.substring(0, i + 1);
break;
}
}
}
}
return value;
}
主要用于按需要长度截取字符串
eg: String_length(obj.getRemark(), 80)