how can i remove chars between indexes in a javascript string

前端 未结 10 2432
无人及你
无人及你 2020-12-17 09:39

i have the following:

var S=\"hi how are you\";
var bindex = 2;
var eindex = 6;

how can i remove all the chars from S that reside between

10条回答
  •  我在风中等你
    2020-12-17 10:13

    The following function returns the complementary result of slice function:

     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) 
     }
    

    examples:

     "hi how are you".slice(2, 6) // " how"
     "hi how are you".remainderOfSlice(2, 6) // "hi are you"
    
     "hi how are you".slice(-2, 6) // ""
     "hi how are you".remainderOfSlice(-2, 6) // "hi how are you"
    

提交回复
热议问题