Javascript 如何删除javascript字符串中索引之间的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5217557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:12:39  来源:igfitidea点击:

how can i remove chars between indexes in a javascript string

javascript

提问by scatman

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 bindex and eindex?
therefore S will be "hi are you"

如何从 S 中删除位于 bindex 和 eindex 之间的所有字符?
因此 S 将是“你好”

采纳答案by Anurag

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, spliceout the unwanted part and convert to string again.

另一种方法是将字符串转换为数组,splice去掉不需要的部分并再次转换为字符串。

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

回答by Rasmus Striib

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

取 bindex 之前的文本并与 eindex 之后的文本连接,例如:

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"

S 现在是“你好”

回答by Alex Pacurar

try

尝试

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

回答by lonesomeday

With String.slice:

String.slice

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

回答by CertainPerformance

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

不需要创建任何中间数组或字符串一种解决方法是使用.replace获取一组中的第一个字符,匹配您要删除的角色,并与第一捕获组取代:

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

回答by CloudyMarble

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

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

回答by Don

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)

  1. 从 bindex 和 eindex 获取子字符串
  2. 从该字符串中删除空格
  3. 重建字符串

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

回答by ChrisBrownie55

DON'T USE SLICE; TRY SPLICE

不要使用切片;尝试拼接

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

虽然slice很好,但它的设计就像substring,它旨在获取东西,而不是删除东西。

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"

回答by Gyumeijie

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"