赞
踩
题目:
如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1
否则如果字符 Unicode 编码 > 255 则长度为 2
解答:
function strLength(s, bUnicode255For1) {
if(bUnicode255For1===true){
return s.length;
}else{
let len = 0;
for(let i=0; i<s.length;i++){
if(s.charCodeAt(i)>255){
len +=2;
}else{
len++;
}
}
return len;
}
}
格式:str.charCodeAt(index)
知识点:判断Unicode编码长度、charCodeAt()方法
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。