js计算字符串的字节

来源:互联网 发布:甘肃精准扶贫数据平台 编辑:程序博客网 时间:2024/05/21 06:27

方法一:

str.replace(/[^\x00-\xff]/g, 'xx').length 

方法二:

String.prototype.byteLen = function(){             var len = 0,             i = this.length;             while(i--)                 {                     len += (this.charCodeAt(i)>255 ? 2 : 1);                 }             return len; }调用:str.byteLen ()