how can i remove chars between indexes in a javascript string

First find the substring of the string to replace, then replace the first occurrence of that string with the empty string.

S = S.replace(S.substring(bindex, eindex), "");

Another way is to convert the string to an array, splice out the unwanted part and convert to string again.

var result = S.split('');
result.splice(bindex, eindex - bindex);
S = result.join('');

Take the text before bindex and concatenate with text after eindex, like:

var S="hi how are you"; 
var bindex = 2; var eindex = 6; 
S = S.substr(0, bindex) + S.substr(eindex);

S is now "hi are you"

Tags:

Javascript