Languages
[Edit]
EN

TypeScript - remove substring from string between indexes

0 points
Created by:
Paluskitten
356

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