indexOf & lastIndexOf to get location of a string
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)
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.
Output of above code is 16 as l position is counted up to the last occurrence.
var my_str="Welcome to www.plus2net.com"
Subscribe to our YouTube Channel here
This article is written by plus2net.com team.