Home > Web Front-end > JS Tutorial > Detailed explanation of commonly used interception string substr(), substring(), slice() methods_javascript skills

Detailed explanation of commonly used interception string substr(), substring(), slice() methods_javascript skills

WBOY
Release: 2016-05-16 15:25:26
Original
1950 people have browsed it

slice()

Definition: Accepts one or two parameters, the first parameter specifies the starting position of the substring. The second parameter represents the end position of the substring (excluding the character at the end position). If the second parameter is not passed, the length of the string is used as the end position.

1. When the passed parameter is a positive value:

var str ="helloWorld";

// 一个参数,则将字符串长度作为结束位置
alert(str.slice(3)); // "loWorld"

// 两个参数,7位置上的字符为"r",但不包括结束位置的字符
alert(str.slice(3,7)); // "loWo"

Copy after login

2. When the passed parameter is a negative value:

The

slice() method adds the passed negative value to the length of the string.

var str ="helloWorld";

// 一个参数,与字符串长度相加即为slice(7)
alert(str.slice(-3)); // "rld"

// 两个参数,与字符串长度相加即为slice(3,6)
alert(str.slice(3,-4)); // "loW"

Copy after login

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

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

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

4. IE compatibility

When tested with the IE8 browser, there is no problem and the behavior is consistent with modern browsers.

substring()

Definition: Accepts one or two parameters, the first parameter specifies the starting position of the substring. The second parameter represents the end position of the substring (excluding the character at the end position). If the second parameter is not passed, the length of the string is used as the end position.

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

var str ="helloWorld";

// 一个参数,则将字符串长度作为结束位置
alert(str.substring(3)); // "loWorld"

// 两个参数,7位置上的字符为"r",但不包括结束位置的字符
alert(str.substring(3,7)); // "loWo"

Copy after login

2. When the passed parameter is a negative value:

substring()方法会把所有负值参数转换为0。来看下例子:

var str ="helloWorld";

// 两个参数,-4会转换为0,相当于substring(3,0) -->即为 substring(0,3)
alert(str.substring(3,-4)); // "hel"

Copy after login
The

substring() method will use the smaller number as the starting position and the larger number as the ending position. As in the above example, substring(3,0) and substring(0,3) have the same effect.

4. IE compatibility

When tested with the IE8 browser, there is no problem and the behavior is consistent with modern browsers.

substr()

Definition: Accepts one or two parameters, the first parameter specifies the starting position of the substring. The second parameter is somewhat different from the previous method, indicating the number of characters returned. If no second argument is passed, the length of the string is used as the ending position. Let’s see an example:

1. When the passed parameter is a positive value:

var str ="helloWorld";

// 一个参数,则将字符串长度作为结束位置
alert(str.substr(3)); // "loWorld"

// 两个参数,从位置3开始截取后面7个字符
alert(str.substr(3,7)); // "loWorld"

Copy after login

2. When the passed parameter is a negative value:

The

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

var str ="helloWorld";

// 将第一个负的参数加上字符串的长度--->
//即为:substr(7,5) ,从位置7开始向后截取5个字符
alert(str.substr(-3,5)); // "rld"

// 将第二个参数转换为0
// 即为:substr(3,0),即从位置3截取0个字符串,则返回空
alert(str.substr(3,-2)); // ""

Copy after login

3. IE compatibility

The substr() method will have problems when passing negative values, and the original string will be returned. IE9 fixes this issue.

Summary

The behavior of slice() and substring () is consistent when passing positive parameters, and the substr() method will be easily confused on the second parameter

When passing negative parameters, the slice() method adds the length of the string, which is in line with general thinking. Converting the second parameter of substring() to 0 will easily cause problems, and the starting position will be easy to change.

IE compatibility issues may occur when the substr() method has a negative value.

To sum up, I generally recommend using the slice() method.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template