In JavaScript, getting the substring after a specific character is useful in cases where you know the format of the input and want to get a specific output.
For example, if you have a string like this:
JAVASCRIPT
const input = "1) Hello World";
You might want to get the substring after the first ) character.
In this post, we'll learn the easiest way to get the substring after a specific character.
Getting the substring after a specific character
Let's start again with out example input string.
JAVASCRIPT
const input = "1) Hello World";
If we want to get the substring after the first ) character, we first need to get the index of that character.
To get this, we can use the built-in string method indexOf().
This method takes a character and returns the index of that character, which is the number of characters before the character.
JAVASCRIPT
const input = "1) Hello World";
const index = input.indexOf(")");
Now we can use this information to get the substring after it. We can use the substring() method to get the substring after the index that we just calculated.