Is there a method to remove a substring from a string?

2

For now I use the replace method for this but I wanted to know if there is an exclusive method to do this:

pontos = '${parseInt(pontos) + aposta}${pontos.replace(parseInt(pontos), "")}';
    
asked by anonymous 28.09.2017 / 15:23

1 answer

4

It does not exist, but you can make one. Just add to the prototype of type String that all strings, both existing and those that will still be instantiated, will have it.

String.prototype.remover = function (input) {   
    var output = this;
    while (output.indexOf(input) > -1) { // só porque o replace não é global.
        output = output.replace(input, "");
    }
    return output;
}

Note that this method does not change the string, but returns a new one.

And to test, after running the above code, you can do in the console:

"abcde".remover("c");
    
28.09.2017 / 15:33