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.
const removeSubstring = (text: string, startIndex: number, endIndex: number): string => {
if (endIndex < startIndex) {
startIndex = endIndex;
}
const a = text.substring(0, startIndex);
const b = text.substring(endIndex);
return a + b;
};
// Usage example:
// index: 0 5 7 11 14
// | | | | |
const text: string = 'This is my text';
console.log(removeSubstring(text, 0, 5)); // is my text
console.log(removeSubstring(text, 5, 7)); // This my text
console.log(removeSubstring(text, 1, text.length - 1)); // Tt
Output:
is my text
This my text
Tt