JavaScript intercepts Chinese and English strings

Source: Internet
Author: User

Sometimes it may be too long to display a paragraph of text, affecting the display of our page. If only English, then we can use the string.substring (start, end) function is sufficient. But usually we all encounter both English and Chinese characters. While the Chinese characters are occupied 2 bytes, if using string.substring (start, end) to intercept the string, you will find that after the interception of Chinese characters after the same number of English after interception, but the length of a lot of long (depending on the number of characters to determine). Fanzhi County Grain Bureau

Here is a function to implement this function:

<script language= "javascript" type= "Text/javascript" >  var str = "Welcome to Concise modern Magic";d Ocument.write (Cutstr (str, 8) );//js intercepts strings, both Chinese and English can be used  //If the given string is greater than the specified length, the specified length is truncated, and no returns the source string.  //String, length    /**  * js intercept string, both English and Chinese can use  * @param str: The string to intercept  * @param len: Length required to intercept */  function Cutstr (Str,len)  {  var str_length = 0;  var str_len = 0;  Str_cut = new String ();  Str_len = str.length;  for (var i = 0; i < Str_len; i++)  {  a = Str.charat (i);          str_length++;          if (Escape (a). length > 4)          {           //The length of the Chinese character is encoded greater than 4           str_length++;     }      Str_cut = Str_cut.concat (a);      if (Str_length>=len)       {     str_cut = Str_cut.concat ("...");           return str_cut;        }  }      If the given string is less than the specified length, the source string is returned;      if (Str_length < len) {       return  str;  }  }  </script>  

You can also do this by using a custom JavaScript native method.

<script language= "javascript" type= "Text/javascript" >//Calculate string length String.prototype.strLen = function () {var len = 0;  for (var i = 0; i < this.length; i++) {if (This.charcodeat (i) > 255 | | this.charcodeat (i) < 0) Len + 2; else len ++;} return Len;} Splits the string into characters, coexisting in the array String.prototype.strToChars = function () {var chars = new Array (); for (var i = 0; i < this.length; i++) {Chars[i] = [This.substr (i, 1), This.ischs (i)];} String.prototype.charsArray = Chars;return chars;} Determines whether a character is a Chinese character String.prototype.isCHS = function (i) {if (This.charcodeat (i) > 255 | | this.charcodeat (i) < 0) return TR Ue;elsereturn false;} Intercept string (from start byte to end byte) String.prototype.subCHString = function (start, end) {var len = 0;var str = ""; This.strtochars (); for (var i = 0; i < this.length; i++) {if (this.charsarray[i][1]) len + 2;else len++;if (end < Len) return str ; else if (Start < len) str + = this.charsarray[i][0];} return str;} Intercept a string (intercept length bytes from the start byte) String.prototype.subCHStr = function (Start, Length) {return this.subchstring (start, start + length);} var str = "Welcome ABC access to concise Modern Magic"; var str1 = str.subchstr (0, 9); var str2 = Str.subchstr (2, 4);   Alert (str1 + "= =" + str2);d ocument.write (str1);</script>

It is very simple to use, just like string.substring (Start, end). Like what:

var str = "Welcome ABC access to concise Modern magic"; var str1 = Str.subchstr (2, 2); var str2 = str.subchstring (2, 4); Alert (str1 + "= =" + str2);

JavaScript intercepts Chinese and English strings

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.