js获取字符串长度;英文占1个字符,中文占2个字符

//计算字符串长度,英文占1个字符,中文占2个字符
function getStrLength(str){
var strLength = 0;
for(var i=0;i<str.length;i++){
if (str.charCodeAt(i)>127 || str.charCodeAt(i)==94) {
strLength += 2;
} else {
strLength ++;
}
}
return strLength;
}

posted @ 2018-08-09 10:05  大鹰  阅读(185)  评论(0编辑  收藏  举报