方法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); } }
以上就是js获取提交的字符串的字节数代码示例的详细内容。
