
//1、原型定义如下:
if (!String.prototype.splice) {
String.prototype.splice = function(start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount))
}
}
//2、重写后代码如下:
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem))
}
//3、使用方法:
var result = "foo baz".splice(4, 0, "bar ")
document.body.innerHTML = result
//结果:在 "foo bar"中的第4个位置插入字符串bar ,变成:"foo bar baz"
<script>function str_add(str,length,pvalue)
{
var re = new RegExp("\\w{1,"+length+"}","g")
ma = str.match(re)
return ma.join(pvalue) + pvalue
}
console.log(str_add("000100020003",4,'b'))
</script>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)