Often use the Intercept string substr (), substring (), slice () method to explain _javascript skills

Source: Internet
Author: User

Slice ()

Definition: Accepts one or two arguments, and the first parameter specifies the start position of the substring. The second parameter represents the end position of the substring (excluding the character at the end position), and if no second argument is passed, the length of the string is used as the ending position.

1, pass parameter is positive condition:

var str = "HelloWorld";

A parameter, the string length is used as the end position
alert (Str.slice (3));//"Loworld"

//two arguments, and the character at 7 position is "R", but does not include the character alert at the end position
( Str.slice (3,7)); "Lowo"

2, pass the parameter is negative condition:

The slice () method adds the Passed-in negative value to the length of the string.

var str = "HelloWorld";

A parameter, added to the string length, is slice (7)
alert (Str.slice ( -3));//"Rld"

//two parameters, added to the string length is slice (3,6)
alert ( Str.slice (3,-4)); "Low"

3, the second argument is less than the first parameter value:

The slice () method returns an empty string if the second argument passed in is smaller than the first argument.

var str = "HelloWorld";
Alert (Str.slice (5,3)); // ""

4, IE compatibility

In the IE8 browser test, there is no problem, behavior and modern browser consistent.

SUBSTRING ()

Definition: Accepts one or two arguments, and the first parameter specifies the start position of the substring. The second parameter represents the end position of the substring (excluding the character at the end position), and if no second argument is passed, the length of the string is used as the ending position.

1, pass parameter is positive condition: and slice () method behavior is same

var str = "HelloWorld";

A parameter, the string length is used as the end position
alert (str.substring (3));//"Loworld"

//two arguments, and the character at 7 position is "R", but does not include the character alert at the end position
( Str.substring (3,7)); "Lowo"

2, pass the parameter is negative condition:

The substring () method converts all negative parameters to 0. Take a look at the example:

var str = "HelloWorld";

Two parameters,-4 will be converted to 0, equivalent to substring (3,0)--> is substring (0,3)
alert (str.substring (3,-4));//"Hel"

The substring () method takes a smaller number as the starting position and a larger number as the ending position. As the above example substring (3,0) and substring (0,3) are the same effect.

4, IE compatibility

In the IE8 browser test, there is no problem, behavior and modern browser consistent.

SUBSTR ()

Definition: Accepts one or two arguments, and the first parameter specifies the start position of the substring. The second argument is somewhat different from the previous method, indicating the number of characters returned. If the second argument is not passed, the length of the string is used as the ending position. See examples:

1, pass parameter is positive condition:

var str = "HelloWorld";

A parameter, the string length is taken as the end position
alert (STR.SUBSTR (3)),//"Loworld"

//two parameters, and the 7 character
alert (Str.substr (3,7)) is intercepted from position 3. ; "Loworld"

2, pass the parameter is negative condition:

The substr () method adds a negative first argument to the length of the string and a negative second argument to 0.

var str = "HelloWorld";

The first negative parameter is added to the length of the string--->
/is: substr (7,5), 5 character
alert (Str.substr ( -3,5)) is intercepted from position 7;//"Rld"

// Converts the second argument to 0//is
: substr (3,0), which intercepts 0 strings from position 3, then returns null
alert (STR.SUBSTR (3,-2));//""

3, IE compatibility

A problem occurs when the substr () method passes a negative value, and the original string is returned. The IE9 fixes the problem.

Summarize

The slice () and substring () behavior are consistent when the positive parameter is passed, and the substr () method is easily confused on the second parameter

In the case of passing negative arguments, the slice () method is added by the length of the string, conforming to the general thinking, substring () The second argument to 0 can be problematic and the starting position can be easily changed.

An IE compatibility problem occurs when the substr () method is negative.

in summary, the younger brother generally recommends using the slice () method.

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.