Home > Web Front-end > JS Tutorial > body text

js indexOf() definition and usage_basic knowledge

WBOY
Release: 2016-05-16 17:48:52
Original
1673 people have browsed it

Returns the character position of the first substring occurrence in the String object.

strObj.indexOf(subString[, startIndex])

Parameters
strObj

Required. String object or literal.

subString

Required. The substring to find in the String object.

starIndex

Optional. This integer value indicates the index within the String object at which to begin the search. If omitted, the search is from the beginning of the string.

Description
The indexOf method returns an integer value indicating the starting position of the substring within the String object. If the substring is not found, -1 is returned.

If startindex is negative, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.

Perform the search from left to right. Otherwise, the method is the same as lastIndexOf.

Example

The following example illustrates the use of the indexOf method.

Copy code The code is as follows:

function IndexDemo(str2){
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.indexOf(str2);
return(s);
}


lastIndexOf method returns the last substring The position of one occurrence, opposite to indexof.

substring method

str=str.substring(int beginIndex); intercept the string whose length is beginIndex from the first letter of str, and assign the remaining string to str;
str =str.substring(int beginIndex, int endIndex); intercept the string from beginIndex to endIndex in str and assign it to str;

Example:
Copy code The code is as follows:

var elems=node.getElementsByTagName("*");
for(var i=0 ;iif(elems[i].className.indexOf(classname)!=-1)
{ }
}

a .indexOf(b) can be used to determine a match and returns the position of the first letter of b in a, or -1 if not.
If there are repeated characters, the first character shall prevail.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template