js获取提交的字符串的字节数

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

js获取提交的字符串的字节数

  2020-05-12 我要评论
方法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);
}
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们