JavaScript gets the length of Chinese characters and intercepts the character code

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.