EN
TypeScript - get substring of string
0
points
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