public class Char_cn {
public static void main(String[] args) {
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
java中判断字段真实长度(中文2个字符,英文1个字符)的方法
1、判断字符串是否为连续的中文字符(不包含英文及其他任何符号和数字): Regex.IsMatch(“中文”,”^[/u4e00-/u9fa5]”); 2、判断字符串是否为中文字符串(仅不包含英文但可以包含其他符号及数字): !Regex.IsMatch(“中文”,@”[a-zA-Z]”);