Javascript examples for String:indexOf
The indexOf() method returns the position of the first occurrence of a specified value in a string.
The indexOf() method is case sensitive.
Parameter | Description |
---|---|
searchvalue | Required. The string to search for |
start | Optional. Default 0. At which position to start the search |
A Number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs
The following code shows how to Search a string for "welcome":
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {/* ww w . j a v a 2 s . c o m*/ var str = "Hello world test test test"; var n = str.indexOf("e", 5); document.getElementById("demo").innerHTML = n; } </script> </body> </html>