stringObject.substring(start,stop) Used to extract the characters between two specified subscripts in the string.
startRequired. A nonnegative integer that specifies the position in stringObject of the first character of the substring to be extracted.
stop is optional. A nonnegative integer that is one position in the stringObject that is one more than the last character of the substring to be extracted. If this parameter is omitted, the returned substring will go to the end of the string.
start starts from 0 and ends with stop (not including stop). Negative parameters are not accepted.
stringObject.substr(start,length)Can extract the specified number of characters starting from the start subscript in the string
start Required. The starting index of the substring to be extracted. Must be a numeric value. If negative, this parameter 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 to last character, and so on.
length Optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of stringObject is returned.
stringObject.slice(start,end)Extract a certain part of the string and return the extracted part as a new string
start To The starting index of the extracted fragment. If it is a negative number, this parameter specifies the position starting from the end of the string. That is, -1 refers to the last character of the string, -2 refers to the second to last character, and so on.
end The index immediately following the end of the segment to be extracted. If this parameter is not specified, the substring to be extracted includes the string from start to the end of the original string. If this parameter is negative, it specifies the position from the end of the string.
Return the new string including all characters of the string stringObject from start (including start) to end (excluding 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”
If there is only one parameter among the three n will return the remaining string starting from the nth position (calculating the position starting from 0)
If there are two parameters n, m, slice and substring, it will return from the nth position. The string at the m-th position (not including the m-th position), and substr will return m characters starting from the n-th 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)); //”"(空字符串)
When the parameter is a negative value, slice will add the incoming negative value to the string length (string.length), substr will add the negative first parameter to the string length, and the second are converted to 0, substring will convert all negative values to 0.
IE's JavaScript implementation has a problem when handling negative values passed to the substr() method, and it returns the original string.
The above is the detailed content of Usage difference analysis of three common ways to intercept strings in javascript. For more information, please follow other related articles on the PHP Chinese website!