Because: the length property of the String class in JavaScript and the substr () method can determine and intercept the number of characters, but there is no way to deal with the number of bytes (it is well known that Chinese characters account for two bytes, only one in English)
Result: There are differences in placeholder space on the page for Chinese and English characters. Chinese web developers do not fits, and they intercept the same length of characters in both Chinese and English.
There are roughly two ways to deal with Chinese and English characters on the Internet. One is to escape the Chinese characters and then calculate the byte length. One is to use a regular expression to convert Chinese characters to 2-byte symbols, and then calculate the length
Recommend the second method, simple and clear, fast, high efficiency.
In the second method, the following extensions are made to the string type of javascript:
A. String.len () method to calculate the number of string bytes:
string.prototype.len=function() { returnthis. replace (/[^\x00-\xff] /g, "RR"). length; }
Two. String.sub (Endindex) method, starting from 0 to intercept the number of bytes endindex length.
String.prototype.sub =function(n) {varr =/[^\x00-\xff]/G; if( This. replace (R, "MM"). Length <= N)return This; //n = n-3; varm = Math.floor (N/2); for(varI=m; i< This. length; i++) { if( This. substr (0, I). Replace (R, "MM"). length>=N) {return This. substr (0, i); } } return This; };
Three. According to the Unicode encoding range
String.prototype.gblen = function () {var len = 0 for (var i=0; I<this . Length; I++ this . charCodeAt (i) >127 | | this . charCodeAt (i) ==94) {Len + = 2 else {len ++; }} return Len; }
Reference: http://blog.csdn.net/testcs_dn/article/details/21412303
charCodeAt ()
Definition and usage
The charCodeAt () method returns the Unicode encoding of the character at the specified position. This return value is an integer between 0-65535.
Method charCodeAt () is similar to the operation performed by the CharAt () method, except that the former returns the encoding of the character at the specified position, and the latter returns a character string.
Character encoding: http://www.cnblogs.com/darr/p/5355178.html
English character length and truncation processing in JavaScript