Languages
[Edit]
EN

TypeScript - get substring of string

0 points
Created by:
Richard-Bevan
413

In this article, we would like to show you how to get a substring of the string in TypeScript.

Quick solution:

const text: string = 'ABCD';
const substring: string = text.substring(1, 3); // start from index 1 up to 3

console.log(substring); // BC

 

Practical example

In this example, we present different cases of how to get a substring from the text.

const text: string = 'ABCDE';

const substring1 = text.substring(0, 3); // ABC
const substring2 = text.substring(3, 5); // DE
const substring3 = text.substring(3);    // DE   (from index 3 to the end of the string)

console.log(substring1); // ABC
console.log(substring2); // DE
console.log(substring3); // DE

Output:

ABC
DE
DE
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