In the JS character intercept function has the commonly used three slice (), substring (), substr (), below I will introduce to you slice (), substring (), substr () function in the character interception Some of the usage and differences of the time.
stringobject.substring (start,stop) is used to extract the character of a string intermediary between two specified subscripts.
Start Required. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the stringobject.
Stop is optional. A nonnegative integer that is 1 more than the last character of the substring to extract in Stringobject. If this argument is omitted, the returned substring continues until the end of the string.
Start starts from 0 to stop (does not contain a stop) and does not accept negative arguments.
stringobject.substr (start,length) to extract a specified number of characters from the start subscript in a string
Start Required. The starting subscript for the substring to extract. Must be numeric. If it is a negative number, the argument declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on.
Length is optional. The number of characters in the substring. Must be numeric. If this argument is omitted, then the string from the beginning of the stringobject to the end is returned.
Stringobject.slice (start,end) extracts a portion of a string and returns the extracted part with a new string
Start subscript of the fragment to extract. If it is a negative number, the parameter specifies where to start from the end of the string. That is,-1 refers to the last character of the string, 2 refers to the second-lowest character, and so on.
End subscript at the end of the fragment to be extracted immediately. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If the parameter is negative, it specifies the position from the end of the string.
Returns a new string that includes the string Stringobject all characters from start (including start) to end (not including end)
String.slice () string.substring () string.substr () var stringvalue = "Hello World"; alert (Stringvalue.slice (3)); "Lo World" alert (stringvalue.substring (3)); "Lo World" alert (STRINGVALUE.SUBSTR (3)); "Lo World" alert (Stringvalue.slice (3,7)); "Lo w" alert (stringvalue.substring (3,7)); "Lo w" alert (STRINGVALUE.SUBSTR (3,7)); "Lo worl"
Three if there is only one parameter n will return the remaining string from the nth position (starting at 0 to calculate the position)
If there are two parameters N,m,slice and substring will return from the nth position to the first m position (excluding the position m), and Substr will return the M characters starting at the nth position.
———————————————————————————————
String.slice () string.substring () string.substr () var stringvalue = "Hello World"; alert (Stringvalue.slice ( -3)); "Rld" alert (stringvalue.substring ( -3)); "Hello World" alert (STRINGVALUE.SUBSTR ( -3)); "Rld" alert (Stringvalue.slice (3,-4)); "Lo w" alert (stringvalue.substring (3,-4)); "Hel" alert (STRINGVALUE.SUBSTR (3,-4)); "" (empty string)
When the argument is negative, slice adds the passed-in negative value to the string length (string.length), Substr adds the negative first argument to the string length, the second to 0, andsubstring converts all negative values to 0.
The JavaScript implementation of IE has a problem with handling negative values to the substr () method, which returns the original string.
The difference between substring (), substr (), slice () in JavaScript