everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
20 lines (19 loc) • 809 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringSplice = void 0;
/**
* Like Array.splice but for strings. Removes and/or inserts text at a given position.
*
* Example: stringSplice("abcdef", 2, 2, "ZZ") → "abZZef"
*
* @author @dailker
* @param {string} str - The original string.
* @param {number} start - The index at which to start changing the string.
* @param {number} deleteCount - The number of characters to remove.
* @param {string} [insert=""] - The string to insert at the start position.
* @returns {string} The resulting string after splicing.
*/
function stringSplice(str, start, deleteCount, insert = '') {
return str.slice(0, start) + insert + str.slice(start + deleteCount);
}
exports.stringSplice = stringSplice;