Detailed explanation and difference of slice(), substr() and substring()

1. slice (x, y)

slice(x,y) takes a part of the string and returns a new string. Where x is the start position index value, y is the end position, excluding y. You can understand x, y as [x, y). Among them, the values ​​of x and y have the following situations.

① There is only one parameter.
At this time, it will be intercepted from the beginning of the parameter subscript to the end of the string.

var str = "Hello world!";
console.log(str.slice(3));
// lo world!

② x<y
will start at x and end at y, excluding y.

var str = "Hello world!";
console.log(str.slice(3, 8));
// lo wo

③ x>y
will return an empty string at this time.

var str = "Hello world!";
console.log(str.slice(8, 3));
// <empty string>

④ If y is a negative number
, the value of y will be added to the length of the string first, and then the result will be used as the new y value. Then refer to the two cases 2 and 3 above, and give the results according to the situation.

var str = "Hello world!";
console.log(str.length);
// 12
console.log(str.slice(3, -12));	// -> consol.log(str.slice(3, (-12 + 12));
// <empty string>
console.log(str.slice(3, -2));	// -> consol.log(str.slice(3, (-2 + 12));
// lo worl

2. substr(x,y)

substr(x,y) intercepts part of a string and returns a string. The difference is that x is the starting position index value and y is the interception length . Because the x and y parameters have different meanings, it is not meaningful to distinguish between x and y. Therefore, the values ​​of x and y exist as follows.

① There is only one parameter.
At this time, it will be intercepted from the beginning of the parameter subscript to the end of the string.

var str = "Hello world!";
console.log(str.substr(3));
// lo world!

② There are two parameters.
At this time, the string is intercepted according to x as the starting position index value and y as the interception length.

var str = "Hello world!";
console.log(str.substr(3, 5));
// lo wo

③ y<0
will return an empty string at this time.

var str = "Hello world!";
console.log(str.substr(3, -5));
// <empty string>

3. substring(x,y)

substring(x,y) its role is to extract substrings of a string. Like slice(x,y), x is the starting position index value, and y is the ending position, excluding y. You can understand x, y as [x, y). Among them, the values ​​of x and y have the following situations.

① There is only one parameter.
At this time, it will be intercepted from the beginning of the parameter subscript to the end of the string.

var str = "Hello world!";
console.log(str.substring(3));
// lo world!

② x<y
will start at x and end at y, excluding y.

var str = "Hello world!";
console.log(str.substring(3, 8));
// lo wo

③ When x>y
, the positions of x and y will be automatically exchanged, and the result will be given according to the second case.

var str = "Hello world!";
console.log(str.substring(8, 3));
// lo wo

④ x=y
will return an empty string at this time.

var str = "Hello world!";
console.log(str.substring(3, 3));
// <empty string>

⑤ y<0
When y is negative, the value of y will be regarded as 0, and the result will be given according to the third case. In other words, all strings before x (excluding x) will be extracted.

var str = "Hello world!";
console.log(str.substring(3, -5));
// Hel

The difference between the three methods

1. The slice and substring parameters are respectively (start position index value, end position index value), and the parameter of substr is (start position index value, interception length);
2. slice cannot reverse the parameter order, substring can;
3. When the second parameter of slice and substring is negative, the result is different.

Guess you like

Origin blog.csdn.net/qq_40662765/article/details/106037117