EN
TypeScript - remove substring from string between indexes
0 points
In this post, we can find a simple way of how to create our own function to remove substring from a string in TypeScript.
xxxxxxxxxx
1
const removeSubstring = (text: string, startIndex: number, endIndex: number): string => {
2
if (endIndex < startIndex) {
3
startIndex = endIndex;
4
}
5
6
const a = text.substring(0, startIndex);
7
const b = text.substring(endIndex);
8
9
return a + b;
10
};
11
12
13
// Usage example:
14
15
// index: 0 5 7 11 14
16
// | | | | |
17
const text: string = 'This is my text';
18
19
console.log(removeSubstring(text, 0, 5)); // is my text
20
console.log(removeSubstring(text, 5, 7)); // This my text
21
console.log(removeSubstring(text, 1, text.length - 1)); // Tt
Output:
xxxxxxxxxx
1
is my text
2
This my text
3
Tt