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.
Copy codeThe Code is as follows:
Function IndexDemo (str2 ){
Var str1 = "BABEBIBOBUBABEBIBOBU"
Var s = str1.indexOf (str2 );
Return (s );
}
The lastIndexOf method returns the position of the last occurrence of the substring, which is opposite to indexof.
Substring Method
Str = str. substring (int beginIndex); truncates the string whose length is beginIndex from the first letter, and assigns the remaining string to str;
Str = str. substring (int beginIndex, int endIndex); truncate the string from beginIndex to endIndex in str and assign it to str;
Example:
Copy codeThe Code is as follows:
Var elems = node. getElementsByTagName ("*");
For (var I = 0; I <elems. length; I ++ ){
If (elems [I]. className. indexOf (classname )! =-1)
{}
}
A. indexOf (B) can be used to determine the matching. Return the position of B in the first letter of a. If-1 is not returned.
If repeated characters appear, the first character shall prevail.