开发者

how can i remove chars between indexes in a javascript string

i have the following:

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

how can i remove all the chars from开发者_JAVA百科 S that reside between bindex and eindex?

therefore S will be "hi are you"


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"


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('');


try

S = S.substring(0, bindex)+S.substring(eindex);


With String.slice:

S = S.slice(0, bindex) + S.slice(eindex);


A solution that doesn't require creating any intermediate arrays or strings is to use .replace to capture the first characters in a group, match the characters you want to remove, and replace with the first captured group:

// keep first 3 characters, remove next 4 characters
const s = "hi how are you";
console.log(
  s.replace(/(.{3}).{4}/, '$1')
);


S.split(S.substring(bindex, eindex)).join(" ");


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)


DON'T USE SLICE; TRY SPLICE

While slice is good and all, it was designed like substring, it was designed to get stuff, not remove stuff.

Caveat: splice was written for Arrays.

Good news: strings are easily turned into Arrays.

String.prototype.splice = function(start, deleteCount) {
  const newStringArray = this.split('')
  newStringArray.splice(start, deleteCount)
  return newStringArray.join('')
}

'Hello World'.splice(2, 5)
// Output -> "Heorld"


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"


Want to delete multiple ranges in a string at once? It can be tricky because indexes will change as you cut out pieces of a string, but here's something quick that might work depending on your requirements:

function deleteRangesInString(ranges, string, deleteChar = "▮") {
    ranges.forEach(r => {
        string = string.substring(0, r[0]) + deleteChar.repeat((r[1]-r[0])+1) + string.substring(r[1]+1);
    })
    return string.replace(new RegExp(deleteChar, 'g'), '');
}

var s = 'hi how are you, look at me now, look at me now';

console.log(
deleteRangesInString([[2,9],[14,29]], s)
);


Assuming you have only the beginning index bindex and length of the string you want to remove while the end index is indeterminate.

Typical use case for this is in cryptography, where you want to remove salt from a decrypted string (that was inserted at a known index before encryption) without exposing the value of the salt; you can expose the salt length rather than the salt value.

function removeSalt(str, bindex, saltlen) {
    
    let front = str.slice(0, bindex)
    return front + str.slice(front.length + saltlen)
}

var s = "how was45r7% your day?" // can be result from a decryption function
console.log (removeSalt(s, 7, 5))

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜