JavaScript strings intercept substring, substr, and slice

Source: Internet
Author: User

This article describes in detail the substring (), substr (), and Slice () three JS string interception methods in JavaScript, and the substring () method is used to extract the character of a string intermediary between two specified subscripts. substr method is used to return a substring of the specified length starting at the specified position.

substring

method is used to extract the character of a string intermediary between two specified subscripts

SUBSTRING (start,end)
Parameters Describe
Start Necessary. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
Stop Optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject.

If this argument is omitted, the returned substring continues until the end of the string.

return value

A new string value that contains a substring of stringobject whose contents are all characters from start to stop-1, with a length of stop minus start.

Description

The substring returned by the substring method includes the character at start, but does not include the character at end.
If start is equal to end, then the method returns an empty string (that is, a string of length 0).
If start is larger than end, then the method swaps the two parameters before extracting the substring.
If start or end is negative, it will be replaced with 0.

2.substr Method

Definition and usage

The Substr method is used to return a substring of the specified length starting at the specified position.

Grammar

stringObject.substr(start [, length ])
Parameters Describe
Start Necessary. The starting position of the desired substring. The index of the first character in the string is 0.
Length Optional. The number of characters that should be included in the returned substring.

Description

If start is a negative number, then Start=str.length+start.
If length is 0 or negative, an empty string is returned.
If this argument is not specified, the substring continues to the end of the stringobject.

First, they all receive two parameters, slice and substring receive the start position and end position (excluding the end position), and Substr receives the starting position and the string length to be returned. Look directly at the following example:

var test = ‘hello world‘;alert(test.slice(4,7));             //o walert(test.substring(4,7));         //o walert(test.substr(4,7));            //o world

Here's one thing to keep in mind:substring is the starting position with the smaller of the two parameters, and the larger parameter as the end position.

alert(test.substring(7,4));          //o w

Then, when the received parameter is negative, slice adds the length of its string to the corresponding negative number, and the result as a parameter; substr simply adds the first argument to the length of the string as the first argument, and substring simply converts the negative argument directly to 0. The test code is as follows

var test = ‘hello world‘;alert(test.slice(-3));         //rldalert(test.substring(-3));     //hello worldalert(test.substr(-3));        //rldalert(test.slice(3,-4));       //lo walert(test.substring(3,-4));   //helalert(test.substr(3,-4));      //空字符串

1 Note: IE has incorrect handling of negative values for substr, it returns the original string.

JavaScript strings intercept substring, substr, and slice

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.