how can i remove chars between indexes in a javascript string

前端 未结 10 2452
无人及你
无人及你 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:17

    You can:

    1. get the substring from bindex and eindex
    2. remove spaces from that string
    3. rebuild the string

      var new_s = S.slice(1, bindex) + S.slice(bindex, eindex).replace(/\s/g, '') + S.slice(eindex)

提交回复
热议问题