public static int 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;
}
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;
}
本文介绍了一种计算包含中文字符的字符串长度的方法,通过正则表达式匹配中文字符并计算其长度,实现中文字符的特殊计数。
的方法&spm=1001.2101.3001.5002&articleId=8586472&d=1&t=3&u=38144c74bbf0496d8c8862c676e9a219)
692

被折叠的 条评论
为什么被折叠?



