The substr (), the substring (), the Slice () method, and sometimes the use of confusion are often used when intercepting strings, so it is summarized.
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 that character at the end position), and if the second argument is not passed, the length of the string is the end position.
1, passing parameters are positive conditions:
var str = "HelloWorld"; // a parameter, the string length is used as the end position // "Loworld" // two parameters, character "R" at 7 position, but no end position // "Lowo"
2, the transmission parameter is negative value case:
The slice () method adds the passed-in negative values to the string length.
var str = "HelloWorld"; // a parameter, added to the length of the string, is slice (7) // "Rld" // two parameters, added to the string length, is slice (3,6) // "Low"
3. The second parameter is smaller than the first parameter value:
The slice () method returns an empty string if the second parameter passed in is smaller than the first argument.
var str = "HelloWorld"; alert (Str.slice (// ""
4. IE compatibility
In the IE8 browser test, no problem, the behavior is consistent with the modern browser.
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 that character at the end position), and if the second argument is not passed, the length of the string is the end position.
1. The pass parameter is a positive condition: the same as the slice () method behavior
var str = "HelloWorld"; // a parameter, the string length is used as the end position // "Loworld" // two parameters, character "R" at 7 position, but no end position // "Lowo"
2, the transmission parameter is negative value case:
The substring () method converts all negative arguments to 0. Take a look at the example:
var str = "HelloWorld"; // two parameters, 4 will be converted to 0, equivalent to substring (3,0)--substring (0,3) // "Hel"
The substring () method takes the smaller number as the starting position and the larger number as the end position. As the above example substring (3,0) is as effective as substring (0,3).
4. IE compatibility
In the IE8 browser test, no problem, the behavior is consistent with the modern browser.
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 a second argument is not passed, the length of the string is used as the end position. See Example:
1, passing parameters are positive conditions:
var str = "HelloWorld"; // a parameter, the string length is used as the end position // "Loworld" // two parameters, starting from position 3 to intercept the next 7 characters // "Loworld"
2, the transmission parameter is negative value case:
The substr () method adds the first argument of the negative to the length of the string, and the second argument with a negative is converted to 0.
var str = "HelloWorld"; // Add the first negative argument to the length of the string---> // This is : substr (7,5), which intercepts 5 characters from position 7 // "Rld" // convert the second argument to 0 // That is : substr (3,0), which is to intercept 0 strings from position 3, returns an empty // ""
3. IE compatibility
If the substr () method passes a negative value, there is a problem, and the original string is returned. IE9 fixed this issue.
Summarize
The slice () and substring () behaviors are consistent in the case of passing positive parameters, and the substr () method is easily confused on the second parameter
In the case of passing a negative value parameter, the slice () method is added by the length of the string, conforms to the general thinking, substring () The second argument to 0 is prone to problems, the starting position is easily changed,
IE compatibility issues occur when the substr () method is negative.
In general, younger brother generally recommends the use of the slice () method.
------------------------------------------------------
Analysis of string manipulation methods slice, substr, substring and their IE compatibility