标签:return 字符串 code char doc pre length meta htm
<!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>
标签:return 字符串 code char doc pre length meta htm
原文地址:http://www.cnblogs.com/mx2036/p/7000133.html