Strings are immutable in Javascript - you cannot change them "in place".
You will need to cut the original line and return a new line made from all parts:
function replaceAt(s, n, t) {
return s.substring(0, n) + t + s.substring(n + 1);
}
NB: I did not add this to String.prototype, because in some browsers the performance is very poor if you add functions to the prototypebuilt-in types.
source
share