How can I replace a substring of a string given the starting position and the length?
I was hoping for something like this:
var string = "This is a test string";
string.replace(10, 4, "replacement");
so that string
would equal
"this is a replacement string"
..but I can't find anything like that.
Any help appreciated.
Like this:
var outstr = instr.substr(0,start)+"replacement"+instr.substr(start+length);
You can add it to the string's prototype:
String.prototype.splice = function(start,length,replacement) {
return this.substr(0,start)+replacement+this.substr(start+length);
}
(I call this splice
because it is very similar to the Array function of the same name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With