如何从javascript中的分割字符串中删除逗号(,)?
我在 JavaScript 中使用 split 函数来分割字符串。 这是这样,
test= event_display1.split("#");
这工作完美,给我输出为,
Event Name : test1
Location : test, test, test
,
Event Name : test2
Location : test, test, test
,
但我希望我的输出为
Event Name : test1
Location : test, test, test
Event Name : test2
Location : test, test, test
当我分割值时,它在我用作分割字符的字符后设置逗号。
如何从输出值中删除这个逗号?
I have used split function to split the string in javascript.
which is as,
test= event_display1.split("#");
This works perfectly and giving me output as,
Event Name : test1
Location : test, test, test
,
Event Name : test2
Location : test, test, test
,
But i want my output as
Event Name : test1
Location : test, test, test
Event Name : test2
Location : test, test, test
When i split the value it set comma after the character which i have used as split character.
How can i remove this comma from my output value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
默认情况下,
Array
的.toString()
将使用逗号作为分隔符,只需使用以下内容:无论您将什么作为参数传递给
join
将用作分隔符。By default the
.toString()
of anArray
will use comma as its delimiter, just use the following:Whatever you pass as the argument to
join
will be used as the delimiter.正如评论中提到的,查看输入非常有用。我将根据您的输出假设一个结构。您可以在拆分数组后从数组中删除逗号,如下所示:
rwz 在拆分字符串之前修剪字符串的答案肯定更简单。对于这个例子,可以这样调整:
As mentioned in the comments it would be really useful to see the input. I'll assume a structure based on your output. You could remove the commas from the array after you have split it, like this:
rwz's answer of trimming the string before splitting it is definitely simpler. That could be tweaked for this example like this:
如果您的输出字符串由多个字符串组成(使用 \n 字符),您可以使用以下代码删除不需要的逗号:
myString.replace(/\,(?:\n|$)/g, "\n")
If your output string consists of multiple strings (using \n character), you can remove unwanted commas with this code:
myString.replace(/\,(?:\n|$)/g, "\n")