JS: used to determine whether a string contains the indexOf () method, jsindexof
Definition and usage
The indexOf () method returns the position of the first occurrence of a specified string value.
Syntax
stringObject.indexOf(searchvalue,fromindex)
Parameters
|
Description |
Searchvalue |
Required. Specifies the string value to be retrieved. |
Fromindex |
Optional integer parameter. Specifies the start position of the string. The valid value is 0 to stringObject. length-1. If this parameter is omitted, It is retrieved from the first character of the string. |
Description
The indexOf method returns an integer. The character position in the stringObject starts from 0. If no substring is found,-1 is returned.
Example:
<Script type = "text/javascript"> var str = "Hello world! "; Alert (str. indexOf (" world "); // 6if (str. indexOf (" Hello ")! =-1) {alert ("included");} else {alert ("excluded") ;}</script>
Js checks whether a string contains a string
String. indexOf ("Check string")> 0 exists, <= 0 does not exist
Js determines whether a string is contained
If (a. indexOf ('], [') <0)
Does not contain
Else
Include