JavaScript Basics

JavaScript Advanced

JavaScript Arrays

JavaScript Functions

JavaScript Objects

JavaScript DOM

JavaScript String

How to extract a substring from a string in JavaScript?

If you want to extract a substring from a string in JavaScript, then use the substring() method.

The syntax for the substring() method is:

let subString = originalString.substring(startIndex, endIndex);

The substring() method accepts two arguments, startIndex and endIndex.

  • The startIndex points to the index position of the first character that will be included in the substring.
  • The endIndex is not included in the substring; instead, the endIndex - 1 index position is included.

Note: The substring() method does not modify the original string.

The following example explains you how to use the substring() method in JavaScript:

let str = "JavaScript String";
console.log(str.substring(4, 10)); //Script

The following image shows the indexing of each letter present in the string "JavaScript String" This image will help you visualize the working of the substring() method.

JavaScript substring() method example

Depending on the values passed to the startIndex and the endIndex, you will face the following situations:

1. If startIndex is equal to the endIndex then the substring() method returns an empty string.

let str = "JavaScript String";

console.log(str.substring(11, 11)); //""

In this code, the value of startIndex and endIndex is the same; that is why the substring() method returns an empty string.

2. If startIndex is greater than the endIndex, then the substring() method will swap the values of these two arguments.

let str = "JavaScript String";

console.log(str.substring(10, 4)); //Script

In this code, the value of startIndex is 10, which is greater than the endIndex, so the substring() method swaps these two values because of which the str.substring(10, 4) is changed to str.substring(4, 10) and a substring Script is returned.

3. If startIndex or endIndex is less than zero or NaN, then it will be considered as zero.

let str = "JavaScript String";

console.log(str.substring(-5, 10)); //JavaScript

In this code, the value of startIndex is -5, which is less than zero, so the substring() method changes it to 0. The str.substring(-5, 10) changes to str.substring(0, 10) and returns a substring "JavaScript"

4. If the value of startIndex or endIndex is greater than the string's length, their value is changed to the string's length.

let str = "JavaScript String";

console.log(str.substring(11, 20)); //String

In this code, the value of endIndex is 20, which is greater than the length of the original string JavaScript String, so the substring() method changes it to 17. The str.substring(11, 20) changes to str.substring(11, 17) and returns a substring String.

5. If the argument endIndex is not passed to the substring() method, then it will return the substring that goes from the startIndex till the end of the string.

let str = "JavaScript String";

console.log(str.substring(11)); //String

Recommended Posts