Get substring
Slice () substr () substring () does not modify the string itself, they simply return a basic type of string value
var str= ' abcdefghijklmn ';
1. Pass a parameter
If positive: three returns the same result ( from the parameter subscript to the end of the string )
String.slice (3) =string.substr (3) = string.substring (3) =DEFGHIJKLMN
If it is negative:
String.slice ( -3) = String.substr (-3) These two functions convert 3 to 8 (string length plus (-3) =8), which is equivalent to calling slice (8) and substr (8)
SUBSTRING (-3) converts negative numbers to 0 to return string lengths
2. Pass two parameters
SUBSTRING (1,3) from standard 0 to subscript 2 BC
Slice (1,3) from standard 0 to subscript 2 BC
SUBSTR (1,3) starts with subscript 0 to intercept 3 characters (the difference from two) BCD
3. The second parameter is a negative value
SUBSTRING (3,-4) Converts the second argument to 0, and uses it to substring (3,0) Because this method takes the smaller number as the starting position, the larger number as the ending position, and therefore the equivalent of the call to substring (0,3)
SUBSTR (3,-4) also converts the second argument to 0, which means that 0 strings are returned, so it is empty
Slice (3,-4) converts 4 to a value of character length + (-4)
Difference?
1. A positive parameter is the same, from the parameter subscript to the last
2. A negative parameter substring is always converted to 0 slice () substr () to character length + negative value
3. The second negative substring or 0 substr () is also converted to 0, so the output null character
4. Two parameters substr () The second one is the number of characters to intercept
What is the difference between a string intercept slice () substring () substr ()?