Definition and Usage
Definition and usage
The IndexOf () method returns the position of the the ' the ' of the ' the ' a specified string value in a string.
The IndexOf () method returns the position of the specified value at the first occurrence of the string
Syntax
GrammarstringObject.indexOf(searchvalue,fromindex)
Parameter Parameters |
Description Comments |
Searchvalue The value that you want to match |
Required. Specifies a string value to search for Required option. Specify the value you want to find a match for |
Fromindex Starting position |
Optional. Specifies where to start the search Options available. Match the start position |
Tips and Notes
Attention
Note:the indexOf () method was case sensitive!
Note: the IndexOf () method is an exact match
Note:this method Returns-1 If the string value to search for never occurs.
Note: If no matching value is available, return-1 Example
Instance
In this example we'll do different searches within a "Hello world!" string:
In this case, we're going to "Hello world!" String for different lookup matches:
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>
The output of the code above would be:
The result returned is:
0
-1
6
Try-it-yourself Demos
Interactive walkthroughs
IndexOf ()
IndexOf () to search within a string.
How to find and match a string with indexof ()