Topic description
If the second parameter bUnicode255For1 === true, all characters have length 1
else if character Unicode encoding > 255 then length 2
1 function strLength(s, bUnicode255For1) {
2 if (bUnicode255For1=== true ){
3 return s.length;
4 }
5 else {
6 var slen = s.length;
7 for ( var i = 0;i<s. length;i++ ){
8 if (s.charCodeAt(i)>255){ // charCodeAt() returns the code value of the character, the Chinese character code value is greater than 255
9 slen++ ;
10 }
11 }
12 return slen;
13 }
14 }
Source: Niuke.com