EN
TypeScript - get substring after word
0 points
In this article, we would like to show you how to get substring after word in TypeScript.
In this example, we use indexOf()
method to get the index of a word
substring. Then we use slice()
to get the substring from index + length
index to the end of the text
string.
xxxxxxxxxx
1
const text: string = 'This is example text...';
2
const word: string = 'example';
3
4
const index: number = text.indexOf(word); // 8
5
const size: number = word.length; // 7
6
7
const result: string = text.slice(index + size);
8
9
console.log(result); // ' text...'
In this example, we split the text by the word
and get the part after it using pop()
method.
xxxxxxxxxx
1
const text: string = 'This is example text...';
2
const word: string = 'example';
3
4
const array: string[] = text.split(word); // ['This is ', ' text...']
5
const result: string = array.pop(); // ' text...'
6
7
console.log(result); // ' text...'