There are three commonly used character interception functions in js: slice(), substring(), and substr(). Let’s introduce to you the functions of slice(), substring(), and substr() in character interception. Some usages and differences.
stringObject.substring(start,stop)
is used to extract the string between two Specifies the characters between subscripts.
start is required. 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 Section
start The starting index of the segment to be extracted. 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.
Returns the new string including all the characters of 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 n among the three, the remaining string will be returned starting from the nth position (the position is calculated from 0)
If there are two parameters n, m, slice and substring will Returns the string starting at the n-th position and ending at the m-th position (not including the m-th position), while substr will return m characters starting at 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 negative value to the string length (string.length), and substr will add the negative value One argument plus the length of the string, the second 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 entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!