English character length and truncation processing in JavaScript

Source: Internet
Author: User

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

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.