如何在给定起始位置和长度的情况下替换字符串的子字符串?
我希望有这样的事情:
var string = "This is a test string";
string.replace(10, 4, "replacement");
so that string
将等于
"this is a replacement string"
..但我找不到类似的东西。
任何帮助表示赞赏。
像这样:
var outstr = instr.substr(0,start)+"replacement"+instr.substr(start+length);
您可以将其添加到字符串的原型中:
String.prototype.splice = function(start,length,replacement) {
return this.substr(0,start)+replacement+this.substr(start+length);
}
(我称之为splice
因为它与同名的Array函数非常相似)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)