remove a substring from a string at a specific index in typescript

To remove a substring from a string at a specific index in TypeScript, you can concatenate the portion of the original string before the index with the portion of the original string after the index plus the length of the substring to remove.

Here's an example TypeScript function that removes a substring at a specific index:

index.ts
function removeSubstringAtIndex(input: string, index: number, substringToRemove: string): string {
  return input.substring(0, index) + input.substring(index + substringToRemove.length);
}
189 chars
4 lines

Here's an example usage of the function:

index.ts
const inputString = "Hello World";
const indexToRemove = 6;
const substringToRemove = "World";

const result = removeSubstringAtIndex(inputString, indexToRemove, substringToRemove);
console.log(result); // Output: "Hello"
222 chars
7 lines

In this example, the function removes the "World" substring from the "Hello World" input string at index 6, resulting in the "Hello" output string.

gistlibby LogSnag