function getLen( str) {
var totallength=0;
var totallength=0;
for (var i=0;i<str.length;i++)
{
var intCode=str.charCodeAt(i);
{
var intCode=str.charCodeAt(i);
if (intCode>=0&&intCode<=128) {
totallength=totallength+1; //非中文单个字符长度加 1
}
else {
totallength=totallength+2; //中文字符长度则加 2
}
} //end for
totallength=totallength+1; //非中文单个字符长度加 1
}
else {
totallength=totallength+2; //中文字符长度则加 2
}
} //end for
return totallength;
}
本文介绍了一个JavaScript函数,该函数用于计算输入字符串中英文字符和中文字符的总长度,并考虑了不同字符类型的长度差异。

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



