Purpose: To calculate the length of a string (1 characters in English, 2 characters in Chinese character)
Method One:
function () { var len = 0; for (var i=0; i<this. Length, i++) { if (this). charCodeAt (i) ==94) { + =2 ; Else { ++; } } return len; }
Method Two:
functionstrlen (str) {varLen = 0; for(vari=0; i<str.length; i++) { varc =str.charcodeat (i); //single byte plus 1 if((c >= 0x0001 && C <= 0x007e) | | (0xff60<=c && c<=0xff9f) ) {len++; } Else{len+=2; } } returnLen;}
Method Three:
varJMZ ={};jmz. GetLength=function(str) {///<summary> Get string actual length, Chinese 2, English 1</summary> ///<param name= "str" > string to get length </param> varReallength = 0, Len = str.length, charcode = 1; for(vari = 0; i < Len; i++) {CharCode=str.charcodeat (i); if(charcode >= 0 && charcode <=) Reallength + = 1; ElseReallength + = 2; } returnreallength;};
Method Four:
var l =var blen = 0for (i=0; i<l; i++if (( Str.charcodeat (i) & 0xff00)! =0+ + +;}
Method Five:
Replace the double-byte with two single-byte and then get the length
function (str) { ifnullreturn 0; if (typeof str! = "string") {+ = ""; } return str.replace (/[^\x00-\xff]/g, "n"). Length;
JS Judge string Length (Chinese length is 2, English length is 1)