Languages
[Edit]
EN

TypeScript - get substring after word

0 points
Created by:
cory
1756

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...'

See also

  1. JavaScript - get substring after word
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join