This is a description in the Javascript authoritative guide. E-text like me can barely understand it. It is not translated, but explained as follows.
String. Substring (From,
To)
Arguments
-
From
-
A nonnegative integer that specifies the position
StringOf the first character of the desired substring.
-
Specify the start position of the string to be obtained, that is, the index (non-negative integer)
-
To
-
A nonnegative optional integer that is one greater than
Position of the last character of the desired substring. If this argument is
Omitted, the returned substring runs to the end of the string.
-
Specifies the end position of the string to be obtained, excluding the characters at this position (non-negative integer, optional, if not specified, returns from the specified start position to the end position of the original string)
String. Slice (
Start,
End)
Arguments
-
Start
-
The string index where the slice is to begin. If negative, this
Argument specifies a position measured from the end of the string. That is,-1
Indicates the last character,-2 indicates the second from last character, and
So on.
-
Specify the start position of the string, that is, the index.
-
End
-
The string index immediately after the end of the slice. If not
Specified, the slice provided des all characters fromStartTo
End of the string. If this argument is negative, it specifies a position
Measured from the end of the string.
-
Specifies the end position of the string to be obtained, excluding the characters at this position (optional, return from the specified start position to the end position of the original string if not specified)
String. Substr (Start,
Length)
Arguments
-
Start
-
The start position of the substring. If this argument is
Negative, it specifies a position measured from the end of the string:-1
Specifies the last character,-2 specifies the second-to-last character, and so
On.
-
Specify the start position of the string, that is, the index.
-
Length
-
The number of characters in the substring. If this argument is
Omitted, the returned substring between des all characters from the starting
Position to the end of the string.
-
Specify the length of the string to be obtained. (optional. If no value is specified, the return value ranges from the specified start position to the end position of the original string)
PS: all three methods return a part of the new string of the original string. All the 2nd parameters are optional and all parameters can be negative integers.
String. Substring (From,
To)
String. Slice (Start,
End)
The two methods are similar. Both specify the start and end positions to return a new string. The return results are the same when the parameters are both positive integers. When the parameters are negative integers,String. Substring (From,
To) Treat all negative integers as 0, whileString. Slice (Start,
End) Adds a negative integer to the length of the string.
String. Substr (Start,
Length)
This method only specifies the length of the new string on the second parameter.String. Slice (Start,
EndAdd the negative integer to the length of the original string.
Example
VaR S = "abcdefg ";
S. substring (1, 4) // returns "BCD"
S. Slice (1, 4) // returns "BCD"
S. substr (1, 4) // returns "bcde"
S. substring (2,-3) // The returns "AB" is actually a smaller parameter of S. substring ().
S. Slice (2,-3) // returns "cd" is actually S. Slice (2, 4)
S. substr (2,-3) // returns "cdef" is actually S. Slice (2, 4)