Get the Substring after a specific Character in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Get the substring after a specific Character #

To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character.

index.js
const str = 'A very_long string'; const after_ = str.substring(str.indexOf('_') + 1); console.log(after_); // 👉️ long string

We used the String.substring method to get a part of the string.

The only argument we passed to the substring method is the start index - the index of the first character to be included in the returned string

We used the String.indexOf method to get the index of the first occurrence of the specific character.

However, we don't want to include the character in the returned string, so we increment its index by 1.

The indexOf method returns -1 if the string does not contain the specified character.

If the indexOf method returns -1 in the example, we would add 1 to -1 and call the substring method with a starting index of 0, which would return the entire string.

Indexes are zero-based in JavaScript, therefore the index of the first character in the string is 0 and the index of the last character is str.length - 1.

Alternatively, you can use the str.split() method.

To get the substring after a specific character:

  1. Use the split() method to split the string on the character.
  2. Access the array of strings at index 1.
  3. The first element in the array is the substring after the character.
index.js
const str = 'A very_long string'; const after_ = str.split('_')[1]; console.log(after_); // 👉️ long string

We used the String.split() method to split the string on each occurrence of an underscore.

index.js
const str = 'A very_long string'; // 👇️ ['A very', 'long string'] console.log(str.split('_'));

The second element in the array is the substring after the specified character (underscore in the example).

If the separator is not found in the string, the split() method returns an array containing only 1 element consisting of the entire string.

index.js
const str = 'A very long string'; // 👇️ ['A very long string'] console.log(str.split('_'));

If we access the second element in the array, we would get an undefined value.

One way to handle this is to use the logical OR (||) operator to return the entire string if the separator is not found.

index.js
const str = 'A very long string'; const after_ = str.split('_')[1] || str; console.log(after_); // 👉️ A very long string

We used the logical OR (||) operator to provide a fallback value incase the value to the left of the operator is falsy (e.g. undefined).

If the separator is not contained in the string, the expression returns the entire string.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.