String methods help you to work with strings.
Finding a String in a String
The indexOf() method returns the index of the position of the 1st occurrence of a specified text in a string:
Example:
Example
Both methods accept a second parameter as the starting position for the search.
Searching for a String in a String
The search()method searches a string for a specified value and returns the position of the match:
Example:
Finding a String in a String
The indexOf() method returns the index of the position of the 1st occurrence of a specified text in a string:
Example:
<body>
<p id="p1">Please locate where 'locate' occurs!</p>
<button onclick="myFunction()">read it</button>
<p id="demo"></p>
<script>
function myFunction() { var str =
document.getElementById("p1").innerHTML;
var pos =
str.indexOf("locate");
document.getElementById("demo").innerHTML = pos;
}
</script>
</body>
The last IndexOf()method returns the index of the last occurrence of a specified text in a string:<p id="p1">Please locate where 'locate' occurs!</p>
<button onclick="myFunction()">read it</button>
<p id="demo"></p>
<script>
function myFunction() { var str =
document.getElementById("p1").innerHTML;
var pos =
str.indexOf("locate");
document.getElementById("demo").innerHTML = pos;
}
</script>
</body>
Example
<body>
<p id="p1">Please locate where 'locate' occurs!</p>
<button onclick="myFunction()">read it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str =
document.getElementById("p1").innerHTML;
var pos =
str.lastIndexOf("locate"); document.getElementById("demo").innerHTML = pos;
}
</script>
</body>
Both the indexOf(), and the lastIndexOf() methods return -1 if the text is not found. JavaScript counts positions from zero.
0 is the first position in a string, 1 is the second, 2 is the third......<p id="p1">Please locate where 'locate' occurs!</p>
<button onclick="myFunction()">read it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str =
document.getElementById("p1").innerHTML;
var pos =
str.lastIndexOf("locate"); document.getElementById("demo").innerHTML = pos;
}
</script>
</body>
Both methods accept a second parameter as the starting position for the search.
Searching for a String in a String
The search()method searches a string for a specified value and returns the position of the match:
Example:
<body>
<p id="p1">Please locate where 'locate' occurs!</p>
<button onclick="myFunction()">read it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str =
document.getElementById("p1").innerHTML;
var pos =
str.search("locate");
document.getElementById("demo").innerHTML = pos;
}
</script>
</body>
Did You Notice?<p id="p1">Please locate where 'locate' occurs!</p>
<button onclick="myFunction()">read it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str =
document.getElementById("p1").innerHTML;
var pos =
str.search("locate");
document.getElementById("demo").innerHTML = pos;
}
</script>
</body>
The two methods, indexOf() and search(), are equal. They accept the same arguments (parameters) and they return the same value.
The two methods are equal, but the search() method can take much more powerful search values.