编码格式不同,导致中文转换 字符转为字节数不同;
Unicode 一个中文字符 等于2个字节;
UTF-8 一个中文字符 等于3个字节 (最常用的编码格式, 适用于网络传输)
还有听说, UBuntu(3) 和windows(2) 系统不同 转换的字节数也不同;
public static void main(String[] args) {
// TODO Auto-generated method stub
String haha = "呵呵呵呵abcd";
int true_num = String_length(haha);
System.out.println("true" + true_num);
int false_num = haha.length();
System.out.print("flase" + false_num);
}
public static int String_length(String 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;
}
}
return valueLength;
}
输出结果true12
false8