描述
如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1
否则如果字符 Unicode 编码 > 255 则长度为 2
示例1
输入:
'hello world, 牛客', false复制输出:
17
解答
function strLength(s, bUnicode255For1) {
if(bUnicode255For1===true){
return s.length
}else{
let num=0
for(let i=0;i<s.length;i++){
if(s.charCodeAt(i)>255){
num+=2
}else{
num++
}
}
return num
}
}
注意:s.charCodeAt(i)是用来获取asc2值得