In-depth analysis of JavaScript string operation methods slice, substr, substring and IE compatibility _ javascript skills

Source: Internet
Author: User
This article mainly introduces the JavaScript string operation method slice, substr, substring and relevant information about its IE compatibility. For more information, see substr () and substring (), slice () method, sometimes obfuscation between usage, so to sum up.

Reading directory
• Slice ()
• Substring ()
• Substr ()
• Conclusion

Slice ()

Definition: one or two parameters are accepted. The first parameter indicates the starting position of the sub-string. The second parameter indicates the end position of the substring (excluding the character at the end position). If the second parameter is not passed, the length of the substring is used as the end position.

1. When the pass parameter is a positive value:

Var str = "helloWorld"; // a parameter that uses the string length as the end position alert (str. slice (3); // "loWorld" // two parameters. The 7-Position Character is "r", but does not include the character alert (str. slice (3, 7); // "loWo"

2. When the pass parameter is a negative value:

The slice () method adds the incoming negative value to the string length. Var str = "helloWorld"; // a parameter that is added to the string length is slice (7) alert (str. slice (-3); // "rld" // two parameters, which are added to the string length: slice (3, 6) alert (str. slice (3,-4); // "loW"

3. When the second parameter is smaller than the first parameter value:

If the second parameter passed in by the slice () method is smaller than the first parameter, an empty string is returned.

var str ="helloWorld";alert(str.slice(5,3)); // "" 

4. IE compatibility

In the IE 8 browser test, there is no problem, and the behavior is consistent with that of modern browsers.

Substring ()

Definition: one or two parameters are accepted. The first parameter indicates the starting position of the sub-string. The second parameter indicates the end position of the substring (excluding the character at the end position). If the second parameter is not passed, the length of the substring is used as the end position.

1. When the pass parameter is a positive value: the behavior of the slice () method is the same

Var str = "helloWorld"; // a parameter that uses the string length as the end position alert (str. substring (3); // "loWorld" // two parameters. The 7-Position Character is "r", but does not include the character alert (str. substring (3, 7); // "loWo"

2. When the pass parameter is a negative value:

The substring () method converts all negative values to 0. Let's take a look at the following example:

Var str = "helloWorld"; // two parameters.-4 is converted to 0, which is equivalent to substring () --> that is, substring () alert (str. substring (3,-4); // "El"

The substring () method uses a small number as the start position and a large number as the end position. In the preceding example, substring () and substring () have the same effect.

4. IE compatibility

In the IE 8 browser test, there is no problem, and the behavior is consistent with that of modern browsers.

Substr ()

Definition: one or two parameters are accepted. The first parameter indicates the starting position of the sub-string. The second parameter is different from the previous method, indicating the number of returned characters. If the second parameter is not passed, the length of the string is used as the end position. Here is an example:

1. When the pass parameter is a positive value:

Var str = "helloWorld"; // a parameter that uses the string length as the end position alert (str. substr (3); // "loWorld" // two parameters. The last seven characters alert (str. substr (3, 7); // "loWorld"

2. When the pass parameter is a negative value:

The substr () method adds the length of the string to the negative first parameter, and converts the negative second parameter to 0.

Var str = "helloWorld"; // Add the first negative parameter to the string length ---> // substr ), truncates 5 characters (str. substr (-); // "rld" // convert the second parameter to 0 // substr (), that is, truncates 0 strings from position 3, then, empty alert (str. substr (3,-2 ));//""

3. IE compatibility

If the substr () method transmits a negative value, the original string is returned. IE9 fixed this issue.

Summary

When a positive value parameter is passed, slice () and substring () are the same behavior. The substr () method is confusing in the second parameter.

When the negative value parameter is passed, the slice () method is to add the string length to conform to the general idea. The second parameter of substring () is converted to 0, which may cause problems and change the starting position easily, if the substr () method is negative, the IE compatibility problem may occur.

In summary, we recommend using the slice () method.

The following describes the differences between slice, substr, and substring.

First, they both receive two parameters. slice and substring receive the start position and end position (excluding the end position), while substr receives the start position and the length of the string to be returned. Let's look at the following example:

 var test = 'hello world';  alert(test.slice(,));    //o w  alert(test.substring(,));   //o w  alert(test.substr(,));   //o world 

Note that substring uses the smaller of the two parameters as the starting position and the larger parameters as the ending position.

For example:

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

Then, when the received parameter is a negative number, slice will add the length of its string to the corresponding negative number, and the result will be used as the parameter; substr simply uses the result after adding the first parameter to the string length as the first parameter. substring simply converts the negative parameter to 0. The test code is as follows:

Var test = 'Hello world'; alert (test. slice (-); // rld alert (test. substring (-); // hello world alert (test. substr (-); // rld alert (test. slice (,-); // lo w alert (test. substring (,-); // El alert (test. substr (,-); // empty string

Note: The processing of negative values received by substr by IE is incorrect. It returns the original string.

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.