87,922
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<script type="text/javascript">
<!--
function countByteLength(str, cnCharByteLen)
{
var reCnChar = /[^\x00-\xff]/g;
return str.length + (str.match(reCnChar).length * --cnCharByteLen);
}
var str = "哈喽,world!\nHello, world!";
// \n 被识别为一个字符
alert("\n".length);
alert(str);
// 31
alert(countByteLength(str, 3));
//-->
</script>
</body>
</html>
var s="12中文";
var n=0;
for(i=0;i<s.length;i++){
if(s.charCodeAt(i)<128) n++;
else n += 3;
}
alert(n);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head>
<body>
<script type="text/javascript">
<!--
function countByteLength(str, cnCharByteLen)
{
var byteLen = 0;
for (var i=0; i<str.length; i++)
{
alert(str.charAt(i));
if ((/[\x00-\xff]/g).test(str.charAt(i)))
byteLen += 1;
else
byteLen += cnCharByteLen;
alert(byteLen);
}
return byteLen;
}
// 哈喽,world!
// 17
alert(countByteLength("哈喽,world!", 3));
//-->
</script>
</body>
</html>
<textarea maxlength="65535"></textarea>