JS Method for Determining string inclusion
This document describes how to use JavaScript to determine string inclusion. Share it with you for your reference. The details are as follows:
1. Example:
| 1 2 3 4 5 6 7 8 |
Var tempStr = "tempText "; Var bool = tempStr. indexOf ("Texxt "); // Returns an integer greater than or equal to 0. If "Text" is not included, "-1 is returned. If (bool> 0 ){ Document. write ("contains strings "); } Else { Document. write ("does not contain strings "); } |
2. indexOf usage:
| 1 |
StrObj. indexOf (subString [, startIndex]) |
The indexOf Function Method in JavaScript returns an integer indicating the starting position of the substring in the String object. If no substring is found,-1 is returned. If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.
Parameters:
StrObj: required, String object or text.
SubString: required. The subString to be searched in the String object.
StarIndex: Optional. This integer indicates the index in the String object. If omitted, it will be searched from the beginning of the string;
If startindex is negative, startindex is treated as zero. If it is larger than the index at the largest character location, it is regarded as the largest possible index.
3. Differences from lastIndexOf:
The lastIndexOf () method is used to retrieve substrings from the end of a string.