This time I will bring you JavaScriptA summary of commonly used interception stringsmethods, what are the precautionsof commonly used interception strings in JavaScript, and the following are practical cases , let’s take a look.
stringObject.substring(start,stop) is 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.
stopoptional. 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) extracts a certain part of the string and returns the extracted part as a new string
start The starting index of the fragment 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.
Return the new string including all 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, it 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 to the mth position (not Including the m-th position) of the string, 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.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
PHPThumb image processing steps detailed explanation
php generates random numbers, letters or a mixture of numbers and letters String
The above is the detailed content of Summary of common methods for intercepting strings in JavaScript. For more information, please follow other related articles on the PHP Chinese website!