做个小程序,没想到在这上面翻车了
某博主的java解决方案
public static int chineseLength(String value){
int valueLength = 0;
String chinese = “[\u0391-\uFFE5]”;/这是汉语的这则表达式/
for (int i =0; i <value.length() ; i++) {
/* 获取一个字符 /
String temp = value.substring(i, i + 1);
/ 判断是否为中文字符 /
if (temp.matches(chinese)) {
/ 中文字符长度为2 /
valueLength += 2;
} else {
/ 其他字符长度为1 */
valueLength += 1;
}
}
return valueLength;
————————————————
CSDN博主「jackliang」
原文链接https://blog.csdn.net/xiqingchun/article/details/9260725