获取字符串的长度

描述:如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1。否则如果字符 Unicode 编码 > 255 则长度为 2

示例1

输入:'hello world, 牛客', false

输出:17

 1 function strLength(s, bUnicode255For1) {
 2  if(bUnicode255For1 ===true){
 3   return s.length
 4  }else{
 5   let len=s.length
 6   for(let i=0;i<len;i++){
 7    if(s.charCodeAt(i)>255){
 8     len++
 9    }
10   }
11   return len
12  }
13 }

 

 

 

 

posted @ 2021-06-12 14:50  icyyyy  阅读(243)  评论(0)    收藏  举报