1

[JavaScript Skills] Three substrings in the interception string, which one do you use

Blog description

The information involved in the article comes from the Internet and personal summary, which is intended to summarize personal learning and experience. If there is any infringement, please contact me to delete it, thank you!

slice() interception

illustrate

By specifying the start and end positions, the extraction of a part of the string, and to new string return portion is extracted.

grammar
stringObject.slice(start, end)
Parameter Description

start (required) : Specifies where to start selection.

If it is a negative number, then it specifies the position from the end of the string.

end (optional) : Specifies where to end the selection, that is, the character subscript at the end.

If this parameter is not specified, the intercepted string contains all characters from start to end. If this parameter is a negative number, then it specifies the characters counted from the end of the array.

Example
var str = "0123456789";
str.slice(0,5); // 01234
str.slice(3);  //3456789
str.slice(3,5);  //34
str.slice(-3);  //789

substring() interception

illustrate

Extracting the character string intermediary between two specified index, it returns a new string .

grammar
stringObject.substring(start, stop)
Parameter Description

start (required) : a non-negative integer , note that it is a non-negative integer.

Identifies the position of the first character of the substring to be extracted in stringObject.

stop (optional ): a non-negative integer , note that it is a non-negative integer.

1 (at least one character) more than the position of the last character of the substring to be extracted in stringObject.

var str = "0123456789";
str.substring(3,5); //34
str.substring(3); //3456789

Notice:

If start and end are equal, then the method returns an empty string (that is, a string with a length of 0).

If start is greater than end, the method will exchange these two parameters before extracting the substring.

If start or end is negative, it will be replaced with 0.

substr() interception

illustrate

The substr method is used for to return a substring of the specified length starting from the specified position. Note here that the second parameter is the length, not the end position.

grammar
stringObject.substr(start, length)
Parameter Description

start (required) : the starting position of the required 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.

var str = "0123456789";
str.substr(3,5); //34567
str.substr(3); //3456789

Notice:

If length is 0 or negative, an empty string will be returned

If length is not specified, the substring will continue to the end of stringObject

If start or length is negative, then it will be replaced with 0

Contrast and memory

In fact, I guess most pym know these three methods, but the difference between these three methods is always a little uncontrollable.

Give a piece of code for comparison.

var str = "0123456789";
str.slice(3,5);  //34
str.substring(3,5); //34
str.substr(3); //3456789

Both slice and substring have the concept of start and end, so when there are start and end parameters, there is not much difference. Substring does not support negative numbers. This is different from slice, and it is generally used based on this point.

Substr only has the number of beginning and beginning. The scene used in this way is specific, and the difference is also the most obvious. I hope that if you can't remember the first two, then this orphan will accept it 😂.

thanks

Universal network

And the hardworking self, personal blog , GitHub test , GitHub

Public Account-Guizimo, Mini Program-Xiaogui Blog


归子莫
1k 声望1.2k 粉丝

信息安全工程师,现职前端工程师的全栈开发,三年全栈经验。