splice-string

April 15, 2021 ยท View on GitHub

Remove or replace part of a string like Array#splice

It correctly handles slicing strings with emoji.

Install

$ npm install splice-string

Usage

import spliceString from 'splice-string';

spliceString('unicorn', 3, 4, 'verse');
//=> 'universe'

spliceString('โค๏ธ๐Ÿด๐Ÿด', 1, 1, '๐Ÿฆ„');
//=> 'โค๏ธ๐Ÿฆ„๐Ÿด'

API

spliceString(string, index, count, insert?)

string

Type: string

index

Type: number

Index to start splicing.

count

Type: number

Number of characters to remove.

insert

Type: string

String to insert in place of the removed substring.