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:
xxxxxxxxxx
1
const text: string = 'ABCD';
2
const substring: string = text.substring(1, 3); // start from index 1 up to 3
3
4
console.log(substring); // BC
In this example, we present different cases of how to get a substring from the text
.
xxxxxxxxxx
1
const text: string = 'ABCDE';
2
3
const substring1 = text.substring(0, 3); // ABC
4
const substring2 = text.substring(3, 5); // DE
5
const substring3 = text.substring(3); // DE (from index 3 to the end of the string)
6
7
console.log(substring1); // ABC
8
console.log(substring2); // DE
9
console.log(substring3); // DE
Output:
xxxxxxxxxx
1
ABC
2
DE
3
DE