如何从javascript中的分割字符串中删除逗号(,)?

发布于 2025-01-01 23:08:45 字数 479 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2025-01-08 23:08:45

默认情况下,Array.toString() 将使用逗号作为分隔符,只需使用以下内容:

var str = test.join("");

无论您将什么作为参数传递给 join 将用作分隔符。

By default the .toString() of an Array will use comma as its delimiter, just use the following:

var str = test.join("");

Whatever you pass as the argument to join will be used as the delimiter.

一直在等你来 2025-01-08 23:08:45

正如评论中提到的,查看输入非常有用。我将根据您的输出假设一个结构。您可以在拆分数组后从数组中删除逗号,如下所示:

var event_display1 = "Event Name : test1#Location : test, test, test#,#Event Name : test2#Location : test, test, test#,#";
var test = event_display1.split("#");

for (var i = event_display1.length - 1; i >= 0; i--) {
   if (test[i] == ",") {
          //Use test.slice(i, 1); if you want to get rid of the item altogether
          test[i] = "";
   }
}

rwz 在拆分字符串之前修剪字符串的答案肯定更简单。对于这个例子,可以这样调整:

event_display1 = event_display1.replace(/#,#/g, "##")
var test = event_display1.split("#");

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:

var event_display1 = "Event Name : test1#Location : test, test, test#,#Event Name : test2#Location : test, test, test#,#";
var test = event_display1.split("#");

for (var i = event_display1.length - 1; i >= 0; i--) {
   if (test[i] == ",") {
          //Use test.slice(i, 1); if you want to get rid of the item altogether
          test[i] = "";
   }
}

rwz's answer of trimming the string before splitting it is definitely simpler. That could be tweaked for this example like this:

event_display1 = event_display1.replace(/#,#/g, "##")
var test = event_display1.split("#");
心如荒岛 2025-01-08 23:08:45

如果您的输出字符串由多个字符串组成(使用 \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")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文