var jmz = {}; jmz.GetLength = function(str) { ///<summary>获得字符串实际长度,中文2,英文1</summary> ///<param name="str">要获得长度的字符串</param> 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; };
alert(jmz.GetLength('测试测试ceshiceshi));
var l = str.length; var blen = 0; for(i=0; i<l; i++) { if ((str.charCodeAt(i) & 0xff00) != 0) { blen ++; } blen ++; }
本人只用过方法三,另外两种未验证。