Extracting String Parts: Substring() Method

The second of three methods for extracting a part of a string is the substring(start, end), which is similar to slice(). Substring method will return the same value, as if you used the slice method:

   var str = "Apple, Banana, Kiwi";
   var res = str.substring(7,13);
   The result of res will be Banana

And like the slice method, if you omit the second parameter, substring() will slice out the rest of the string giving you the same answer:

HTML file: Displayed by browser:
<body>
<p>The substring() method extract a part of a string and returns the extracted parts in a new string:</p>
<p id="demo"></p>
<script>
    var str = "Apple, Banana, Kiwi";
    document.getElementById("demo").innerHTML = str.substring(7);
</script>
</body>

The substring() method extract a part of a string and returns the extracted parts in a new string:

The difference? The substring() cannot accept negative indexes.

BackTable of ContentsNext