删除字符串中某个位置的字符 - javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11116501/
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-24 04:32:40  来源:igfitidea点击:

Remove a character at a certain position in a string - javascript

javascriptstring

提问by starbeamrainbowlabs

Is there an easy way to remove the character at a certain position in javascript?

有没有一种简单的方法可以删除javascript中某个位置的字符?

e.g. if I have the string "Hello World", can I remove the character at position 3?

例如,如果我有字符串"Hello World",我可以删除位置 3 处的字符吗?

the result I would be looking for would the following:

我要寻找的结果如下:

"Helo World"

This question isn't a duplicate of Javascript - remove character from a string, beucase this one is about removing the character at a specific position, and thta question is about removing all instances of a character.

这个问题不是Javascript的副本- 从字符串中删除字符,因为这个问题是关于删除特定位置的字符,而这个问题是关于删除一个字符的所有实例。

回答by Matt

It depends how easyyou find the following, which uses simple String methods (in this case slice()).

这取决于您找到以下内容的难易程度,它使用简单的 String 方法(在本例中为slice())。

var str = "Hello World";
str = str.slice(0, 3) + str.slice(4);
console.log(str)

回答by Ishan Dhingra

You can try it this way!!

你可以试试这个方法!!

var str ="Hello World";
var position = 6;//its 1 based
var newStr = str.substring(0,position - 1) + str.substring(postion, str.length);
alert(newStr);

Here is the live example: http://jsbin.com/ogagaq

这是现场示例:http: //jsbin.com/ogagaq

回答by Alexandre Daubricourt

Turn the string into array, cut a character at specified index and turn back to string

将字符串转成数组,在指定索引处剪切一个字符并转回字符串

let str = 'Hello World'.split('')

str.splice(3, 1)
str = str.join('')

// str = 'Helo World'.

回答by SURENDRANATH SONAWANE

If you omit the particular index character then use this method

如果省略特定的索引字符,则使用此方法

function removeByIndex(str,index) {
  if (index==0) {
      return  str.slice(1)
  } else {
      return str.slice(0,index-1) + str.slice(index);
  } 
}

var str = "Hello world", index=3;
console.log(removeByIndex(str,index));

// Output: "Helo world"

回答by Nikhil D

    var str = 'Hello World';
                str = setCharAt(str, 3, '');
                alert(str);

function setCharAt(str, index, chr)
        {
            if (index > str.length - 1) return str;
            return str.substr(0, index) + chr + str.substr(index + 1);
        }

回答by Nikhil D

you can use substring()method. ex,

你可以使用substring()方法。前任,

var x = "Hello world"
var x = x.substring(0, i) + 'h' + x.substring(i+1);

回答by RamThakur

Hi starbeamrainbowlabs ,

嗨,starbeamrainbowlabs,

You can do this with the following:

您可以使用以下方法执行此操作:

var oldValue = "pic quality, hello" ;
var newValue =  "hello";
var oldValueLength = oldValue.length ;
var newValueLength = newValue.length ;
var from = oldValue.search(newValue) ;
var to = from + newValueLength ;
var nes = oldValue.substr(0,from) + oldValue.substr(to,oldValueLength);
console.log(nes);

I tested this in my javascript console so you can also check this out Thanks

我在我的 javascript 控制台中对此进行了测试,因此您也可以查看此内容,谢谢

回答by artis

var str = 'Hello World',
    i = 3,
    result = str.substr(0, i-1)+str.substring(i);

alert(result);

Value of ishould not be less then 1.

的值i不应该小于1