int count=0;
while(true)
{
try{
str.charAt(count++);
}cath(Exception e){
System.out.println("length= "+(--count));
break;
}
}
while(true)
{
try{
str.charAt(count++);
}cath(Exception e){
System.out.println("length= "+(--count));
break;
}
}
调试欢乐多
str.length();
这种问题估计是面试题吧
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;
}
这样就能够实现求字符串的长度
1.当前字符串.length()
2.用当前字符串的toCharArray()方法转换成char数组,再求数组长度即可...