/*** 计算字符串长度* 参数是要计算的字符串* 返回值当前字符串的长度*/function charactersLen (words) { let tempMapWordNum = {} let doubleByteTotal = 0 // 双字节 let chineseTotal = 0 // 汉字 let singleByteTotal = 0 // 单字节 let numTotal = 0 for (let i = 0; i < words.length; i++) { let c =…
首先看看判断字符串长度的几种方法(英文占1个字符,中文汉字占2个字符) 方法一: function strlen(str) { var len = 0; for (var i = 0; i < str.length; i++) { var c = str.charCodeAt(i); //单字节加1 if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) { len++…
方法1: String.prototype.gblen = function() { var len = 0; for (var i=0; i<this.length; i++) { if (this.charCodeAt(i)>127 || this.charCodeAt(i)==94) { len += 2; } else { len ++; } } return len; } 方法2: function strlen(str){ var len = 0; for (var i=0; i&…
方法一: String.prototype.gblen = function() { var len = 0; for (var i = 0; i < this.length; i++) { if (this.charCodeAt(i) > 127 || this.charCodeAt(i) == 94) { len += 2; } else { len++; } } return len; } 方法二: function getStringLen(Str) { var i, len, cod…