Last updated: Mar 1, 2024
Reading timeยท3 min
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.
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 specific character.
The arguments we passed to the method are:
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.
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
.
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.
const str = 'one two_three_four'; const before_ = str.substring(0, str.lastIndexOf('_')); console.log(before_); // ๐๏ธ 'one two_three'
The String.lastIndexOf() method returns the index of the last occurrence of the character.
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.
This is a three-step process:
String.split()
method to split the string on the character.0
.0
is the part of the string before the specific
character.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.
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.
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.
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials: