How to use substring in JavaScript:- Geniusofstudent

  • The substring() method extracts the characters from a string, between two specified indices, and returns the new sub string.
  • This method extracts the characters in a string between "start" and "end", not including "end" itself.
  • If "start" is greater than "end", this method will swap the two arguments, meaning str.substring(4, 1) == str.substring(1, 4).
  • If either "start" or "end" is less than 0, it is treated as if it were 0.
  • The substring() method does not change the original string.





Code:-


  • <!DOCTYPE html> 

    1. <html> 
    2.  
    3. <head> 
    4.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
    5. </head> 
    6.  
    7. <body> 
    8.  
    9.  <h1>substring()</h1> 
    10.  
    11.  <input type="button" onclick="strfun();" value="Get Answer"> 
    12.  
    13.  <script> 
    14.   function strfun() 
    15.   { 
    16.    var str = "abcdefghijkl"; 
    17.    alert(str.substring(4, 5)); //Split from 4th to 5th character. 
    18.   } 
    19.  </script> 
    20.  
    21. </body> 
    22.  
    23. </html>



    How to use substring in JavaScript:- Geniusofstudent How to use substring in JavaScript:- Geniusofstudent Reviewed by Network security on June 30, 2019 Rating: 5

    No comments:

    Useful Information

    Powered by Blogger.