Get the Substring before a specific Character in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 1, 2024
3 min

banner

# Get Substring before a specific Character in JavaScript

Use the substring() method to get the substring before a specific character.

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'

get substring before specific character

The code for this article is available on GitHub

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

The arguments 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, so the first character in a string has an index of 0 and the last character has an index of str.length - 1.

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

index.js
const str = 'one two_three four'; console.log(str.indexOf('_')); // ๐Ÿ‘‰๏ธ 7

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.

# Get Substring before the last occurrence of a specific Character

If your string contains multiple occurrences of the character and you need to get the substring up to the last occurrence of the character, use the String.lastIndexOf() method instead.

index.js
const str = 'one two_three_four'; const before_ = str.substring(0, str.lastIndexOf('_')); console.log(before_); // ๐Ÿ‘‰๏ธ 'one two_three'

get substring before last occurrence of specific character

The code for this article is available on GitHub

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

index.js
const str = 'one two_three_four'; console.log(str.indexOf('_')); // ๐Ÿ‘‰๏ธ 7 console.log(str.lastIndexOf('_')); // ๐Ÿ‘‰๏ธ 13

The string contains multiple occurrences of an underscore, so we used the method to get the part of the string before the last occurrence of an underscore character.

The lastIndexOf() method returns -1 if the character is not contained in the string.

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

# Get Substring before a specific Character using String.split()

This is a three-step process:

  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'

get substring before specific character using split

The code for this article is available on GitHub

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 String.split() method returns an array containing a single element - the entire string.

index.js
const str = 'one two three four'; // ๐Ÿ‘‡๏ธ ['one two three four'] console.log(str.split('_'));

You can use the String.includes() method to check if a string contains a certain character before calling the String.split() method.

index.js
const str = 'one two three four'; let before = ''; if (str.includes('_')) { before = str.substring(0, str.lastIndexOf('_')); } console.log(before); // ๐Ÿ‘‰๏ธ ''

The String.includes() method returns true if the character is contained in the string and false otherwise.

You can also use the Array.shift() method instead of using string indexing.

index.js
const str = 'one two_three four'; const before_ = str.split('_').shift(); console.log(before_); // ๐Ÿ‘‰๏ธ 'one two'

We used the String.split() method to split the string into an array on the specified character.

index.js
const str = 'one two_three four'; // ๐Ÿ‘‡๏ธ [ 'one two', 'three four' ] console.log(str.split('_'));

The last step is to use the Array.shift() method to remove and return the first array element.

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev