Is there a splice method for strings?

前端 未结 10 2403
你的背包
你的背包 2020-11-30 23:32

The Javascript splice only works with arrays. Is there similar method for strings? Or should I create my own custom function?

The substr(),

10条回答
  •  情书的邮戳
    2020-12-01 00:07

    Here's a nice little Curry which lends better readability (IMHO):

    The second function's signature is identical to the Array.prototype.splice method.

    function mutate(s) {
        return function splice() {
            var a = s.split('');
            Array.prototype.splice.apply(a, arguments);
            return a.join('');
        };
    }
    
    mutate('101')(1, 1, '1');
    

    I know there's already an accepted answer, but hope this is useful.

提交回复
热议问题