标签:js 字符串长度
// GBK字符集实际长度计算 function getStrLeng(str){ var realLength = 0; var len = str.length; var charCode = -1; for(var i = 0; i < len; i++){ charCode = str.charCodeAt(i); if (charCode >= 0 && charCode <= 128) { realLength += 1; }else{ // 如果是中文则长度加2 realLength += 2; } } return realLength; }
// UTF8字符集实际长度计算 function getStrLeng(str){ var realLength = 0; var len = str.length; var charCode = -1; for(var i = 0; i < len; i++){ charCode = str.charCodeAt(i); if (charCode >= 0 && charCode <= 128) { realLength += 1; }else{ // 如果是中文则长度加3 realLength += 3; } } return realLength; }
标签:js 字符串长度
原文地址:http://warmdoll.blog.51cto.com/10881334/1758893