sta*_*abs 52 javascript string
有没有一种简单的方法来删除javascript中某个位置的角色?
例如,如果我有字符串"Hello World"
,我可以删除位置3的字符吗?
我要寻找的结果如下:
"Helo World"
Run Code Online (Sandbox Code Playgroud)
这个问题不是Javascript的重复- 从字符串中删除字符,beucase这个是关于删除特定位置的字符,而问题是关于删除字符的所有实例.
Mat*_*att 81
这取决于您是否容易找到以下内容,它使用简单的String方法(在本例中slice()
).
var str = "Hello World";
str = str.slice(0, 3) + str.slice(4);
Run Code Online (Sandbox Code Playgroud)
Ish*_*gra 13
你可以这样试试!!
var str ="Hello World";
var position = 6;//its 1 based
var newStr = str.substring(0,position - 1) + str.substring(postion, str.length);
alert(newStr);
Run Code Online (Sandbox Code Playgroud)
这是现场的例子:http://jsbin.com/ogagaq
将字符串转换为数组,在指定的索引处剪切一个字符,然后返回字符串
let str = 'Hello World'.split('')
str.splice(3, 1)
str = str.join('')
// str = 'Helo World'.
Run Code Online (Sandbox Code Playgroud)
如果省略特定的索引字符,则使用此方法
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"
Run Code Online (Sandbox Code Playgroud)