Javascript string truncation summary _ javascript skills

Source: Internet
Author: User
This article has a collection of character truncation methods, including common js string truncation, string truncation functions, and common interception and usage. It is very simple and practical. If you need it, you can refer to it. This article summarizes three methods to use Javascript to intercept strings, and also summarizes the two methods to intercept Chinese strings.

1. substring Method

Definition and usage
The substring method is used to extract characters of a string between two specified subscripts.

Syntax
StringObject. substring (start, stop)

Parameter description
Start is required. A non-negative integer that specifies the position of the first character of the substring to be extracted in the stringObject.
Optional. A non-negative integer is 1 more than the position of the last character of the substring to be extracted in the stringObject. If this parameter is omitted, the returned substring ends at the end of the string.

Return Value
A new string. The value of this string contains a substring of stringObject. Its content is all characters from start to stop-1, and its length is stop minus start.

Description
The substring returned by the substring method includes the start character, but not the end character.
If start and end are equal, this method returns an empty string (a string with a length of 0 ).
If start is greater than end, this method swaps the two parameters before extracting the substring.
If start or end is negative, it is replaced with 0.
Use Cases:

var str = "0123456789"; alert(str.substring(0));------------"0123456789" alert(str.substring(5));------------"56789" alert(str.substring(10));-----------"" alert(str.substring(12));-----------"" alert(str.substring(-5));-----------"0123456789" alert(str.substring(-10));----------"0123456789" alert(str.substring(-12));----------"0123456789" alert(str.substring(0,5));----------"01234" alert(str.substring(0,10));---------"0123456789" alert(str.substring(0,12));---------"0123456789" alert(str.substring(2,0));----------"01" alert(str.substring(2,2));----------"" alert(str.substring(2,5));----------"234" alert(str.substring(2,12));---------"23456789" alert(str.substring(2,-2));---------"01" alert(str.substring(-1,5));---------"01234" alert(str.substring(-1,-5));--------"" 

2. substr Method

Definition and usage
The substr method returns a substring of the specified length starting from the specified position.

Syntax
StringObject. substr (start [, length])

Parameter description
Start is required. The starting position of the required substring. The index of the first character in the string is 0.
Length is optional. The number of characters to be included in the returned substring.

Description
If the length is 0 or negative, an empty string is returned.
If this parameter is not specified, the substring will continue to the end of the stringObject.

Use Cases:

var str = "0123456789"; alert(str.substr(0));---------------"0123456789" alert(str.substr(5));---------------"56789" alert(str.substr(10));--------------"" alert(str.substr(12));--------------"" alert(str.substr(-5));--------------"0123456789" alert(str.substr(-10));-------------"0123456789" alert(str.substr(-12));-------------"0123456789" alert(str.substr(0,5));-------------"01234" alert(str.substr(0,10));------------"0123456789" alert(str.substr(0,12));------------"0123456789" alert(str.substr(2,0));-------------"" alert(str.substr(2,2));-------------"23" alert(str.substr(2,5));-------------"23456" alert(str.substr(2,12));------------"23456789" alert(str.substr(2,-2));------------"" alert(str.substr(-1,5));------------"01234" alert(str.substr(-1,-5));-----------"" 

3. Custom Methods

The above two methods can only intercept English letters and numbers. When you encounter Chinese characters, you will be helpless. Forced Use will lead to garbled characters, my authenticated research has finally concluded two ways to use Javascript to intercept strings containing Chinese characters:

Method 1:

String.prototype.sub=function(n){   var r=/[^\x00-\xff]/g;   if(this.replace(r,"mm").length<=n){return this;}   var m=Math.floor(n/2);   for(var i=m;i
 
  =n){       return this.substr(0,i)+"...";     }   }   return this; } 
 

Method 2:
// The Truncation string contains Chinese processing and parameter meanings: (string, truncation length, whether to add ...)

function subString(str, len, hasDot){   var newLength=0;   var newStr="";   var chineseRegex=/[^\x00-\xff]/g;   var singleChar='';   var strLength=str.replace(chineseRegex,'**').length;   for(var i=0;i < strLength;i++){   singleChar=str.charAt(i).toString();   if(singleChar.match(chineseRegex) != null){     newLength+=2;   }else{     newLength++;   }   if(newLength>len){     break;   }   newStr+=singleChar;   }      if(hasDot && strLength>len){     newStr+='...';   }   return newStr; } 

The following describes how to use Js to intercept Chinese strings:

  
  Summary of Javascript string truncation processing (Summary of Chinese strings intercepted by Js) 

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.