Javascript 替换字符串数组Javascript中的字符

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

Replace characters in string array Javascript

javascriptarraysstringreplace

提问by petehallw

I have defined and populated an array called vertices. I am able to print the output to the JavaScript console as below:

我已经定义并填充了一个名为vertices. 我可以将输出打印到 JavaScript 控制台,如下所示:

["v 2.11733 0.0204144 1.0852", "v 2.12303 0.0131256 1.08902", "v 2.12307 0.0131326 1.10733" ...etc. ]

However I wish to remove the 'v' character from each element. I have tried using the .replace()function as below:

但是我希望从每个元素中删除 'v' 字符。我尝试使用以下.replace()功能:

var x;
for(x = 0; x < 10; x++)
{
    vertices[x].replace('v ', '');
}

Upon printing the array to the console after this code I see the same output as before, with the 'v's still present.

在此代码之后将数组打印到控制台时,我看到与以前相同的输出,但 'v 仍然存在。

Could anyone tell me how to solve this?

谁能告诉我如何解决这个问题?

回答by p e p

Strings are immutable, so you just have to re-assign their value:

字符串是不可变的,所以你只需要重新分配它们的值:

vertices[x] = vertices[x].replace('v ', '');

回答by nicael

Should be

应该

vertices[x]=vertices[x].replace('v ', '');

Because replace returnsvalue, and doesn't changeinitial string.

因为替换返回值,并且不会更改初始字符串。

回答by Sahil Nagpal

vertices[x] = vertices[x].replace('v ', '');