题解 | #获取字符串的长度#
获取字符串的长度
http://www.nowcoder.com/practice/e436bbc408744b73b69a8925fac26efc
function strLength(s, bUnicode255For1) {
var total=0
//匹配空格的数目
var space=s.match(/ /g)
var spaceLength=0
for (i in space){
spaceLength+=1
}
console.log(spaceLength)
//判断标识
if(bUnicode255For1===true){
total=s.length+spaceLength
spaceLength=0
}else{
for(var i=0;i<s.length;i++){
//利用charCodeAt(index)的方法获得字符的ascll码
if(s.charCodeAt(i)>255){
total+=2
}else{
total+=1
}
}
spaceLength=0
}
return total
}
格力公司福利 386人发布