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.
1. Using slice()
method
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.
const text: string = 'This is example text...';
const word: string = 'example';
const index: number = text.indexOf(word); // 8
const size: number = word.length; // 7
const result: string = text.slice(index + size);
console.log(result); // ' text...'
2. Using split()
with pop()
method
In this example, we split the text by the word
and get the part after it using pop()
method.
const text: string = 'This is example text...';
const word: string = 'example';
const array: string[] = text.split(word); // ['This is ', ' text...']
const result: string = array.pop(); // ' text...'
console.log(result); // ' text...'