Javascript to get Chinese Character length and extract Chinese character code

Source: Internet
Author: User

This article introduces how to obtain Chinese Character lengths and intercept Chinese Characters in Javascript. For more information, see.

Obtain the length of a string containing Chinese Characters

In actual development and application scenarios, for example, when the number of characters is limited for typographical layout, because a Chinese character occupies two English characters, the String. length is not reliable for calculating the length of a String containing Chinese characters. There are two solutions

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 result 6


Solution 2:

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 result 6

After testing a string with a length of more than 10000, solution 1 takes about 2 ms, and solution 2 takes about 12 ms. Once the solution is successful, it once again reflects the power of regular expressions.


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 = 'Method of occurrence of world Ah sssl ';
Document. write ('str1 = '+ str1 +' <br/> ');
Document. write ('length = '+ str1.gblen () +' <br/> ');
Document. write ('gbtrim (10) = '+ str1.gbtrim (10) +' <br/> ');
Document. write ('gbtrim (10 ,'... ') =' + Str1.gbtrim (10 ,'... ') +' <Br/> ');
Document. write ('gbtrim (12, '-') = '+ str1.gbtrim (12,'-') +' <br/> ');

// Gbtrim (len truncation length, which is calculated based on the length of English bytes. The omitted characters After s truncation, such "..." )
// Note: Chinese characters are calculated as two lengths. Therefore, when len in gbtrim is 10, a maximum of five Chinese characters are displayed.
// When the number of Chinese characters is greater than 5, because "…" is added after the truncation, Therefore, only four Chinese characters are displayed.
// Pay attention to Chinese Encoding
</Script>

Related Article

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.