JS determines the method that a string contains
- 2020-06-07 03:56:55
- OfStack
This article gives an example of how JS determines string inclusion. Share to everybody for everybody reference. The details are as follows:
1. Example:
var tempStr = "tempText" ;
var bool = tempStr.indexOf("Texxt");
// Return greater than or equal to 0 If not included "Text" It returns "-1 .
if(bool>0){
document.write(" Containing string ");
}else{
document.write(" No strings ");
}
indexOf = indexOf
strObj.indexOf(subString[, startIndex])
The indexOf function method in JavaScript returns an integer value indicating the starting position of the substring within the String object. If no substring is found, it returns -1. If startindex is negative, startindex is treated as zero. If it is larger than the largest character position index, it is treated as the largest possible index.
Parameters:
strObj: Required option, String object or text.
subString: Required, the substring to look up in the String object.
starIndex: Optionally, the integer value indicates the index to start searching within the String object. If omitted, it looks at the beginning of the string;
If startindex is negative, startindex is treated as zero. If it is larger than the largest character position index, it is treated as the largest possible index.
3. Differences from lastIndexOf:
The lastIndexOf() method retrieves the substring from the end of the string.
Hopefully, this article has been helpful in your javascript programming.