¿Cómo eliminar la coma (,) de la cadena dividida en javascript?

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?

preguntado el 02 de febrero de 12 a las 11:02

showing the original input and code would be even better -

3 Respuestas

De forma predeterminada, .toString() de una 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.

Respondido 02 Feb 12, 15:02

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("#");

Respondido 02 Feb 12, 15:02

If your output string consists of multiple strings (using \n character), you can remove unwanted commas with this code: myString.replace(/\,(?:\n|$)/g, "\n")

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.