views:

45

answers:

4

I have a Javascript string array with values like A12, B50, C105 etc. and I want to turn it into a pipe delimited string like this: A12|B50|C105...

How could I do this? I'm using jQuery (in case that helps with some kind of builtin function).

+2  A: 
var pipe_delimited_string = string_array.join("|");

Array.join is a native Array method in Javascript which turns an array into a string, joined by the specified separator (which could be an empty string, one character, or multiple characters).

Daniel Vandersluis
+1  A: 

No need for jQuery. Use Javascripts join() method. Like

var arr = ["A12", "C105", "B50"],
    str = arr.join('|');

alert(str);
jAndy
A: 

Use JavaScript 'join' method. Like this:

Array1.join('|')

Hope this helps.

NawaMan
A: 

For a native JavaScript array then myArray.join('|') will do just fine.

On the other hand, if you are using jQuery and the return value is a jQuery wrapped array then you could do something like the following (untested):

jQuerySelectedArray.get().join('|')

See this article for more information.

Garett