<html>
<head>
<script>
function lenByte(tempvalue){
if(tempvalue==null || tempvalue.length==0 || tempvalue=="")
{
return 0;
}else{
var i,sum;
sum=0;
for(i=0;i<tempvalue.length;i++)
{
if ((tempvalue.charCodeAt(i)>=0) && (tempvalue.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}
}
</script>
</head>
<body>
<script>
var str = "中华人民共和国中华人民共和国中华人民共和";
alert(str.length);
alert(lenByte(str));
</script>
</body>
<head>
<script>
function lenByte(tempvalue){
if(tempvalue==null || tempvalue.length==0 || tempvalue=="")
{
return 0;
}else{
var i,sum;
sum=0;
for(i=0;i<tempvalue.length;i++)
{
if ((tempvalue.charCodeAt(i)>=0) && (tempvalue.charCodeAt(i)<=255))
sum=sum+1;
else
sum=sum+2;
}
return sum;
}
}
</script>
</head>
<body>
<script>
var str = "中华人民共和国中华人民共和国中华人民共和";
alert(str.length);
alert(lenByte(str));
</script>
</body>
</html>
来源:http://blog.youkuaiyun.com/zlcoy_1/article/details/7245818
本文介绍了一种计算字符串字节长度的方法,通过JavaScript函数实现,适用于包含中文字符的情况。示例代码展示了如何计算一个含有中文的字符串的字节数。
195

被折叠的 条评论
为什么被折叠?



