在Javascript中,字符串是 不可变的 ,这意味着您可以做的最好的事情是创建一个具有更改内容的新字符串,然后将变量分配为指向它。
您需要自己定义
replaceAt()函数:
String.prototype.replaceAt=function(index, replacement) { return this.substr(0, index) + replacement+ this.substr(index + replacement.length);}并像这样使用它:
var hello="Hello World";alert(hello.replaceAt(2, "!!")); //should display He!!o World



