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


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer