function getLength(str) { var totalLength = 0; var charCode; for (var i = 0; i < str.length; i++) { charCode = str.charCodeAt(i); if (charCode < 0x007f) { totalLength++; } else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) { totalLength += 2; } else if ((0x0800 <= charCode) && (charCode <= 0xffff)) { totalLength += 3; } else{ totalLength += 4; } } return totalLength;}
var oneName = “sodasjd”;alert(oneName);
转载于:https://www.cnblogs.com/aifengguo/p/7028795.html
相关资源:js获取字符串字节数方法小结