Substring (), substr(), slice()
slice(beginSlice[, endSlice])
Slice (beginSlice, endSlice) intercepts a portion of a string and returns a new string. BeginSlice indicates the start position index value, endSlice indicates the end position, and endSlice is not included. “BeginSlice” and “endSlice” can be understood as “beginSlice, endSlice”. The values of “beginSlice” and “endSlice” can be described in the following situations.
-
① There is only one parameter beginSlice
It intercepts from the parameter subscript to the end of the string.
-
(2) beginSlice < endSlice
It’s going to start at x and end at y, not including y.
-
(3) beginSlice > endSlice
An empty string is returned.
-
④ beginSlice/endSlice is negative
If the value is negative, it is treated as sourceLength + beginSlice, where sourceLength is the length of the string (for example, if beginSlice is -3 it is treated as: SourceLength -3), and treat the result as the new value. Finally, refer to the above two cases of 2 and 3, and give the results according to the situation.
str.substr(beginSlice[, length])
Str.substr (beginSlice, length) takes a portion of a string and returns a string. The difference is that beginSlice is the index value of the starting position and length is the interception length. Because the meanings of beginSlice and length are different, it is meaningless to distinguish beginSlice and length from each other. So, beginSlice and Length values exist as follows.
-
① There is only one parameter beginSlice
It intercepts from the parameter subscript to the end of the string.
-
② There are two parameters
In this case, the string is intercepted according to beginSlice as the starting position index value and Length as the interception length.
-
(3) length < 0
An empty string is returned.
substring(beginSlice[, endSlice])
Substring (beginSlice, endSlice) is used to extract substrings of strings. Similar to slice(beginSlice, endSlice), the beginSlice index value is the start position, and endSlice value is the end position, which does not contain endSlice. “BeginSlice” and “endSlice” can be understood as “beginSlice,endSlice”. The values of “beginSlice” and “endSlice” can be described in the following situations.
-
① Only one parameter
It intercepts from the parameter subscript to the end of the string.
-
(2) beginSlice < endSlice
It’s going to start at x and end at y, not including y.
-
(3) beginSlice > endSlice
The beginSlice and endSlice positions are automatically swapped, and the result is given in the second case.
-
(4) beginSlice = endSlice
An empty string is returned.
-
(5) endSlice < 0
When y is negative, the endSlice value is treated as zero, and the third case is given. In other words, all strings up to and without beginSlice are extracted.
The difference between the three methods
1. The slice and substring parameters are (start position index value, end position index value), and the substr parameters are (start position index value, intercept length). 2. Slice cannot reverse parameter order, substring can; 3. Slice and Substring get different results when the second argument is negative.
reference
Original link: blog.csdn.net/qq_21436667…