Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • substring() method in JavaScript String

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 461
    Comment on it

    JavaScript String substring() method : The substring() method is used to extract the sub string between two points. It returns the new substring. If start point is greater than end point then it swaps automatically these points, means substring(4,2) treated as substring(2,4). If any of the parameter is negative or less than zero then it is treated as zero.


    Syntax of substring() method :

    string.substring(start,end)
    

    start : This is required. This is the numeric index used to start point of the sub string.

    end : This is optional parameter. This is numeric index used to stop the extract of characters. Character on this position is not included in sub string.


    Example of substring() method : Following are the examples of the substring() method

    Example1.html

    <!-- Here extracting substring with single parameter-->
    <!DOCTYPE html>
    <html>
    <body>
    <p>To display the substring, click on button.</p>
    <button onclick="displaySubstring()">substring</button>
    <p id="container"></p>
    
    <script>
    function displaySubstring() {
        var msg = "Time to do rest.";
        var result = msg.substring(2);
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : me to do rest.
    

    Example2.html

    <!-- Here extracting substring using start and end parameter.-->
    <!DOCTYPE html>
    <html>
    <body>
    <p>To display the substring, click on button.</p>
    <button onclick="displaySubstring()">substring</button>
    <p id="container"></p>
    
    <script>
    function displaySubstring() {
        var msg = "Time to do rest.";
        var result = msg.substring(2, 4);
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : me
    

    Example3.html

    <!-- In this we try to pass value less than zero.-->
    <!DOCTYPE html>
    <html>
    <body>
    <p>To display the substring, click on button.</p>
    <button onclick="displaySubstring()">substring</button>
    <p id="container"></p>
    
    <script>
    function displaySubstring() {
        var msg = "Time to do rest.";
        var result = msg.substring(-1);
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : Time to do rest.
    


    In the last example we try to pass the negative value, so as we mentioned at starting of this article, it convert it to the zero. So the result is same as msg.substring(0);.

 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: