Last updated: Mar 4, 2024
Reading timeยท4 min
To split a string on the last occurrence of a substring:
lastIndexOf()
method to get the last occurrence of the substring.slice()
method to get the parts before and after the substring.function splitLastOccurrence(str, substring) { const lastIndex = str.lastIndexOf(substring); const before = str.slice(0, lastIndex); const after = str.slice(lastIndex + 1); return [before, after]; } const str = 'bobby#hadz#com'; const [before, after] = splitLastOccurrence(str, '#'); console.log(before); // ๐๏ธ bobby#hadz console.log(after); // ๐๏ธ com
The String.lastIndexOf() method returns the index of the last occurrence of a substring in a string.
The method returns -1
if the substring is not contained in the string.
The String.slice() method extracts a section of a string and returns it, without modifying the original string.
The String.slice()
method takes the following arguments:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
To get the substring before the last occurrence, we called the slice
method
with a start index of 0
and went up to, but not including the index of the
last occurrence.
const str = 'bobby#hadz#com'; const lastIndex = str.lastIndexOf('#'); // ๐๏ธ bobby#hadz console.log(str.slice(0, lastIndex));
To get the substring after the last occurrence, we called the slice()
method
with a single parameter - the start index.
const str = 'bobby#hadz#com'; const lastIndex = str.lastIndexOf('#'); // ๐๏ธ com console.log(str.slice(lastIndex + 1));
We don't want to include the hash #
in the string, we added 1
to the start
index.
When only a single argument is passed to the String.slice()
method, the slice
goes to the end of the string.
If you need to handle a scenario where the substring is not contained in the
string, you can use an if
statement.
function splitLastOccurrence(str, substring) { const lastIndex = str.lastIndexOf(substring); let before = ''; let after = ''; if (lastIndex !== -1) { before = str.slice(0, lastIndex); after = str.slice(lastIndex + 1); } return [before, after]; } const str = 'bobby-hadz-com'; const substring = '#'; const [before, after] = splitLastOccurrence(str, substring); console.dir(before); // ๐๏ธ '' console.dir(after); // ๐๏ธ ''
The lastIndexOf()
method returns -1
if the substring is not contained in the
string.
Our if
statement checks if the lastIndexOf()
method didn't return -1
.
If it didn't, then the substring is contained in the string and we can safely
call the slice
method.
Otherwise, the before
and after
variables would store empty strings.
split()
This is a three-step process:
pop()
method to get the part after the last occurrence.join()
method to get the part before the last occurrence.function splitLastOccurrence(str, substring) { const arr = str.split(substring); const after = arr.pop(); const before = arr.join(substring); return [before, after]; } const str = 'bobby#hadz#com'; const [before, after] = splitLastOccurrence(str, '#'); console.log(before); // ๐๏ธ bobby#hadz console.log(after); // ๐๏ธ com
The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.
The String.split()
method takes the following 2 parameters:
Name | Description |
---|---|
separator | The pattern describing where each split should occur. |
limit | An integer used to specify a limit on the number of substrings to be included in the array. |
We split the string on each occurrence of the substring.
const str = 'bobby#hadz#com'; // ๐๏ธ [ 'bobby', 'hadz', 'com' ] console.log(str.split('#'));
We used the Array.pop()
method to get the part after the last occurrence of
the substring.
const str = 'bobby#hadz#com'; console.log(str.split('#').pop()); // ๐๏ธ com
The pop()
method
removes and returns the last element from the array.
You can use the join()
method to join the remainder of the array with the
substring as a separator to get the part before the last occurrence of the
substring.
function splitLastOccurrence(str, substring) { const arr = str.split(substring); const after = arr.pop(); const before = arr.join(substring); return [before, after]; } const str = 'bobby#hadz#com'; const [before, after] = splitLastOccurrence(str, '#'); console.log(before); // ๐๏ธ bobby#hadz console.log(after); // ๐๏ธ com
The Array.join()
method joins an array into a string based on the provided
delimiter.
console.log(['bobby', 'hadz'].join('#')); // ๐๏ธ bobby#hadz
You can learn more about the related topics by checking out the following tutorials: