获取字符串的长度

来源:互联网 发布:大数据与制造业 编辑:程序博客网 时间:2024/06/02 20:17
如果第二个参数 bUnicode255For1 === true,则所有字符长度为 1

否则如果字符 Unicode 编码 > 255 则长度为 2


function strLength(s, bUnicode255For1) {
      var  count=0;
      if(bUnicode255For1 === true)
      {
               returns.length;
      }
      for(var i=0;i<s.length;i++)
      {
           if(s.charCodeAt(i)>255)
           {
                count=count+2;
           }
           else
                count+=1;
      }
     return count;
}
    添加笔记


0 0