Js determines whether the string contains a certain string, and js determines whether the string contains
Js checks whether a String contains a certain String, searches for sub-characters in the String object, indexOf
Var test = "aa ";
If (test. indexOf ("a")> 0)
{
}
IndexOf usage:
Returns the first occurrence of a substring in a String object.
StrObj. indexOf (subString [, startIndex])
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 it is omitted, It is searched from the beginning of the string.
Description
The indexOf method 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.
Search from left to right. Otherwise, this method is the same as lastIndexOf.
Example
The following example illustrates how to use the indexOf method.
Function IndexDemo (str2 ){
Var str1 = "BABEBIBOBUBABEBIBOBU"
Var s = str1.indexOf (str2 );
Return (s );
}
JavaScript indexOf is case-insensitive.
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.
The indexOf function performs search from left to right. Otherwise, this method is the same as lastIndexOf.
The following example illustrates how to use the indexOf function method.
Function IndexDemo (str2 ){
Var str1 = "BABEBIBOBUBABEBIBOBU"
Var s = str1.indexOf (str2 );
Return (s );
}
Js determines whether a string is contained
If (a. indexOf ('], [') <0)
Does not contain
Else
Include
Js checks whether a string contains a string
String. indexOf ("Check string")> 0 exists, <= 0 does not exist