Method 1:
var jmz = {};jmz.GetLength = function(str) { ///<summary>Get the actual length of the string, Chinese 2, English 1</summary> ///<param name="str">To get the string of length</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('test test ceshiceshi));Method 2 (a more concise method):
var l = str.length;var blend = 0;for(i=0; i<l; i++) {if ((str.charCodeAt(i) & 0xff00) != 0) {blen ++;}blen ++;}Method 3 (a more concise method):
var jmz = {};jmz.GetLength = function(str) { return str.replace(/[/u0391-/uFFE5]/g,"aa").length; //First replace Chinese with two bytes of English, calculate the length}; alert(jmz.GetLength('test test ceshiceshi'));The simple way to get the actual length of a string (including Chinese characters) in the above article is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.