方法1:
function getBytesCount(str)
{
  var bytesCount = 0;
  if (str != null)
  {
    for (var i = 0; i < str.length; i++)
    {
      var c = str.charAt(i);
      if (/^[\u0000-\u00ff]$/.test(c))
      {
        bytesCount += 1;
      }
      else
      {
        bytesCount += 2;
      }
    }
  }
  return bytesCount;
}

方法2:
function getBytesCount2(str)
{
  if (str == null)
  {
    return 0;
  }
  else
  {
    return (str.length + str.replace(/[\u0000-\u00ff]/g, "").length);
  }
}

相关文章:

  • 2022-12-23
  • 2021-07-12
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2021-11-24
  • 2021-07-12
  • 2022-12-23
相关资源
相似解决方案