/**
* 校验字符串是否在规定字符数内
* @param str
* @param maxLength
* @return
*/
public static boolean isOver(String str,Integer maxLength) {
try {
int length = str.getBytes("GBK").length;
if(length > maxLength) {
return false;
}else {
return true;
}
} catch (UnsupportedEncodingException e) {
return false;
}
}
例:
String str = “哈哈哈123”;
str.getBytes(“GBK”).length; //GBK编码的字节数:9
str.getBytes(“UTF-8”).length; //UTF-8编码的字节数:12
str.getBytes(“Unicode”).length; //Unicode编码的字节数:14
注:
GBK:中文2字节,英文1个字节
UTF-8::中文3字节,英文1个字节
Unicode:中文4字节,英文2个字节
原文链接:https://blog.csdn.net/qq_27810387/article/details/103362182