美文网首页
JS 计算字符长度 包括中英文和标点

JS 计算字符长度 包括中英文和标点

作者: 八妹sss | 来源:发表于2021-03-12 10:58 被阅读0次
    
    // Get string width
    function getStringWidth(val) {
      let len = 0;
      for (let i = 0; i < val.length; i++) {
        let length = val.charCodeAt(i);
        if( length >= 0 && length <= 128 ) {
          len += 1;
        } else {
          len += 2;
        }
      }
      return len;
    

    原文链接:https://blog.csdn.net/kadxls/article/details/90374581

    相关文章

      网友评论

          本文标题:JS 计算字符长度 包括中英文和标点

          本文链接:https://www.haomeiwen.com/subject/hozaqltx.html