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
}
京公网安备 11010502036488号