说明:英文字符返回一个长度,中文返回两个长度。
var textObj = {};
textObj.GetLength = function(str) {
str = str.replace(/(^\s*)|(\s*$)/g,"");
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;
};
使用方法
var 变量名称 = textObj.GetLength("字符串");
683

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



