Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • JavaScript indexof() and lastindexof() String Methods

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 157
    Comment on it

    indexof() method
    The indexOf() method gives the position of the first occurrence of a particular text in a string

    Example

    < !DOCTYPE html>

    <p id="see">I am Very good person</p>
    
    <button onclick="mydata()">result</button>
    
    <p id="d"></p>
    
    <script>
    function mydata() {
        var str = document.getElementById("see").innerHTML;
        var pos = str.indexOf("good");
        document.getElementById("d").innerHTML = pos;
    }
    </script>
    
    </body>
    </html>
    

    OUPUT
    10
    lastindexof() method
    The lastIndexOf() method gives the position of the last occurrence of a particular text in a string

    Example

    <!DOCTYPE html>
    <html>
    <body>
    
    <p id="x">Please contact me</p>
    
    <button onclick="myFunction()">result</button>
    
    <p id="y"></p>
    
    <script>
    function myFunction() {
        var str = document.getElementById("x").innerHTML;
        var pos = str.lastIndexOf("c");
        document.getElementById("y").innerHTML = pos;
    }
    </script>
    
    </body>
    </html>
    

    OUPUT
    12

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: