<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>@求一个字符串的字节长度</title>
</head>
<body>
</body>
<script>
function GetBytes(str) {
var len = str.length;
var bytes = len;
for (var i = 0; i < len; i++) {
if (str.charCodeAt(i) > 255) { //判断非常规字符,比如中文
bytes++;
}
}
return bytes;
}
var strs = '无语,abc';
var c = GetBytes(strs);
console.log('c:' + c); //8
</script>
</html>