返回字符长度,一个汉字=3个字节
check.charrs=function(str){
var wordwide = 0;
for (var i=0; i<str.length; i++) {
var c = str.charCodeAt(i);
if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {//单字节加1
wordwide++;
}else {
wordwide+=3;
}
}
return wordwide;
}
</script>
check.charrs=function(str){
var wordwide = 0;
for (var i=0; i<str.length; i++) {
var c = str.charCodeAt(i);
if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {//单字节加1
wordwide++;
}else {
wordwide+=3;
}
}
return wordwide;
}
</script>
本文介绍了一种计算字符串中每个字符所占字节数的方法,特别适用于需要区分单字节和多字节字符(如汉字)的情况。通过一个JavaScript函数实现了这一功能,能够帮助开发者准确获取字符串的实际长度。
3584

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



