To get the length of a string containing Chinese
In practical development applications, such as word limit typesetting, the string.length of string length with Chinese is not reliable due to the position of a Chinese character in two English characters. There are two ways to solve
The code is as follows |
Copy Code |
String.prototype.cnLength = function () { var arr = This.match (/[^x00-xff]/ig); return this.length + (arr = null 0:arr.length); };
Alert ("Hi Hello". cnlength ()); Test Results 6
|
Programme II:
The code is as follows |
Copy Code |
String.prototype.cnLength = function () { var len = 0; var ch; for (var i = 0; i < this.length; i++) { ch = this.charcodeat (i); if (ch >= 0 && ch <= 255) { len++; } else { Len + 2; } } return Len; };
Alert ("Hi Hello". cnlength ()); Test Results 6 |
After a length of 10000+ string test, the scheme is approximately 2ms, and the Scheme II is about 12ms. The scheme is a triumph, once again embodies the strength of the regular expression.
Intercept Chinese characters
The code is as follows |
Copy Code |
<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/> <script type= "Text/javascript" > 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; } String.prototype.gbtrim = function (len, s) { var str = "; var sp = s | | ”; var len2 = 0; for (var i=0; i<this.length; i++) { if (This.charcodeat (i) >127 | | this.charcodeat (i) ==94) { Len2 + 2; } else { Len2 + +; } } if (len2 <= len) { return this; } len2 = 0; Len = (len > sp.length)? Len-sp.length:len; for (var i=0; i<this.length; i++) { if (This.charcodeat (i) >127 | | this.charcodeat (i) ==94) { Len2 + 2; } else { Len2 + +; } if (Len2 > Len) { STR + SP; Break } STR + + This.charat (i); } return str; } var str1 = ' World ah Ah sssl occurrence of the law '; document.write (' str1 = ' + str1 + ' <br/> '); document.write (' length = ' + str1.gblen () + ' <br/> '); document.write (' Gbtrim "= ' + Str1.gbtrim (+) + ' <br/> '); document.write (' Gbtrim, ' ... ') = ' + Str1.gbtrim (Ten, ' ... ') + ' <br/> '); document.write (' Gbtrim, '-') = ' + Str1.gbtrim ('-') + ' <br/> '); Gbtrim (len intercept length, calculated in English byte length, s truncated after the ellipsis character, such as "...") Note: Here the characters are calculated as two lengths, so Len in Gbtrim is 10 o'clock and displays up to 5 characters. When the number of Chinese characters is greater than 5 o'clock, the "..." is added after the interception, so only 4 characters are displayed. Note Chinese coding </script> |