How to replace substring in string by index in JavaScript

1 Answer

0 votes
String.prototype.replaceAt=function(index, ch) {
    return this.substr(0, index) + ch + this.substr(index + ch.length);
}
   
s = "abcd"; 

s = s.replaceAt(1, "XY");

document.write(s);  
 


/*
run:
    
aXYd 
    
*/

 



answered Jun 21, 2019 by avibootz
...