-
getLength = function(str) {
// 先把中文替换成两个字节的英文,再计算长度
return str.replace(/[\u0391-\uFFE5]/g,"aa").length;
}
-
var length = str.length;
var blen = 0;
for (i=0; i<length; i++) {
// Unicode编码,Unicode的前128个字符编码和ASCII是一致的,即向后兼容ASCII
if ((str.charCodeAt(i) & 0xff00) != 0) {
blen ++;
}
blen ++;
}
-
getLength = function(str) {
var realLength = 0, len = str.length, charCode = -1;
for (var i = 0; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128)
realLength += 1;
else
realLength += 2;
}
return realLength;
};
网友评论