Get the Substring before a specific Character in JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Get Substring before a specific Character #

Use the substring() method to get the substring before a specific character, e.g. const before = str.substring(0, str.indexOf('_'));. The substring method will return a new string containing the part of the string before the specified character.

index.js
const str = 'one two_three four'; const before_ = str.substring(0, str.indexOf('_')); console.log(before_); // 👉️ 'one two'

We used the String.substring method to get the substring before a character.

The parameters we passed to the method are:

  1. the start index - the index of the first character to be included in the new string
  2. the end index - go up to, but not including this index
JavaScript indexes are zero-based, which means that the first index in a string is 0 and the last is str.length - 1.

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

If the character is not contained in the string, the indexOf method returns -1.

In that case the substring method would get invoked with (0, -1) and would return an empty string because the substring method treats negative values as if you passed 0.

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

Get Substring before a specific Character using split() #

To get the substring before a specific character:

  1. Use the String.split() method to split the string on the character.
  2. Access the array element at index 0.
  3. The array element at index 0 is the part of the string before the specific character.
index.js
const str = 'one two_three four'; const before_ = str.split('_')[0]; console.log(before_); // 👉️ 'one two'

We used the String.split method to split the string into an array of substrings based on a provided separator.

index.js
const str = 'one two_three four'; // 👇️ ['one two', 'three four'] console.log(str.split('_'));

The array element at index 0 contains the substring before the specified character.

If the character is not contained in the string, the split() method returns an array containing a single element - the entire string.

index.
const str = 'one two three four'; // 👇️ ['one two three four'] console.log(str.split('_'));

Get Substring before last occurrence of specific Character #

Use the lastIndexOf() method to get the substring before the last occurrence of a specific character, e.g. str.substring(0, str.lastIndexOf('_'));. The substring method will return a new string containing the part of the string before the last occurrence of the specified character.

index.js
const str = 'one_two_three_four'; const beforeLast_ = str.substring(0, str.lastIndexOf('_')); console.log(beforeLast_); // 👉️ 'one_two_three'

The String.lastIndexOf method returns the index of the last occurrence of the character.

The lastIndexOf method returns -1 if the character is not contained in the 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.