indexOf & lastIndexOf to get location of a string
<script type="text/javascript">
var my_str="www.plus2net.com"
document.write(my_str.indexOf("plus"))
</script>
The above code will return 4
We can find out the location of a particular string inside the main string by using indexOf function.
The first character of the main string is considered as 0 location.
We will get the position of first occurrence of the matching string.
document.write(my_str.indexOf(".")// Output is 3)
lastIndexOf
This method returns us the last occurrence of the string from the left and the location ( or position ) is given from starting ( left ) side.
Here is the code.
var my_str="Welcome to www.plus2net.com"
document.write(my_str.lastIndexOf("l"))
Output of above code is 16 as l position is counted up to the last occurrence.
←JavaScript String Reference
IndexOf is used to get domain name userid from email address →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
plus2net.com