Replaces a substring at a specific index with a new string

export const replaceAt = function (str: string, index: number, length: number, replacement: string): string {
return str.substring(0, index) + replacement + str.substring(index + length);
};
  • Parameters

    • str: string

      The original string

    • index: number

      The starting index for replacement

    • length: number

      The number of characters to replace

    • replacement: string

      The replacement string

    Returns string

    A new string with the replacement applied