JavaScript取得字符串的长度,完美识别中文,日文。
(charCode >= 0 && charCode <= 128):中文;
(charCode >= 65377 && charCode <= 65439):日文;
亲测可用!
var getLength = function(items){
var realLength=0;
var charCode = -1;
if(items==null||items==""){
realLength=0;
}else{
for(var i=0,j=items.length;i<j;i++){
charCode = items.charCodeAt(i);
if((charCode >= 0 && charCode <= 128)||(charCode >= 65377 && charCode <= 65439)){
realLength+=1;
}else{
realLength+=2;
}
}
}
return realLength;
};
JS精确计算中日文长度
本文介绍了一种使用JavaScript精确计算包含中文和日文字符的字符串长度的方法。该方法通过遍历字符串并检查每个字符的Unicode编码来确定其是否为中文或日文字符,并据此计算长度。
8757

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



