javascript - 有没有Slice()的反函数?

标签 javascript slice

<分区>

如您所知,slice() 返回切片项。例如,"hello".slice(1, 3) 返回“ell”,而不是余数。我很好奇的是:是否有任何函数或方法来获取 slice() 的剩余部分?

最佳答案

 String.prototype.remainderOfSlice = function(begin, end) {

    begin = begin || 0
    end = (end === undefined) ? this.length : end 

    if (this.slice(begin, end) === '') return this + ''
    return this.slice(0, begin) + this.slice(end) 
 }



console.log("hello".slice()) // "hello"
console.log("hello".remainderOfSlice()) // ""

console.log("hello".slice(-3)) // "llo"
console.log("hello".remainderOfSlice(-3)) // "he"

console.log("hello".slice(-3, 0)) // ""
console.log("hello".remainderOfSlice(-3, 0)) // "hello"

console.log("hello".slice(1, 3)) // "el"
console.log("hello".remainderOfSlice(1, 3)) // "hlo"


    

关于javascript - 有没有Slice()的反函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52398284/

相关文章:

javascript - 如何在单击输入时选择组合值

javascript - 如何在video.js中获取当前播放时间

go - 在 Go 中如何从结构的 slice 中删除一个项目

python - Pandas 时间序列多切片

javascript - 'generated' 属性在哪里是允许的?

javascript - 如何从 Javascript 访问 Laravel Helper?

javascript - jTemplates {#for} 在 Internet Explorer 7 中不工作,错误?

python - 在 PyTorch 中将张量的一部分分割成另一个张量的最快方法是什么?

ruby - 组成切片切片

arrays - 如何在 Chapel 中返​​回对数组切片的引用?