Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create comma-delimited string

Tags:

javascript

I'm looking to find a neat way to create a comma-delimited string from an array. This is how I'm doing it now...

for(i=0;i<10;i++)
{
   str = str + ',' + arr[i];
}
str=str.substring(1)
return str;

... but it feels a bit untidy.

like image 749
Urbycoz Avatar asked Mar 02 '11 10:03

Urbycoz


People also ask

How do I create a comma delimited string?

Given a Set of String, the task is to convert the Set to a comma separated String in Java. Approach: This can be achieved with the help of join() method of String as follows. Get the Set of String. Form a comma separated String from the Set of String using join() method by passing comma ', ' and the set as parameters.

What is a comma delimited string?

(adj.) Comma-delimited is a type of data format in which each piece of data is separated by a comma. This is a popular format for transferring data from one application to another, because most database systems are able to import and export comma-delimited data.

How do you make a comma-separated string in Python?

How to Convert a Python List into a Comma-Separated String? You can use the . join string method to convert a list into a string. So again, the syntax is [seperator].

How do I convert a list to a comma-separated string in Excel?

Please follow these instructions: 1. Type the formula =CONCATENATE(TRANSPOSE(A1:A7)&",") in a blank cell adjacent to the list's initial data, for example, cell C1. (The column A1:A7 will be converted to a comma-serrated list, and the separator "," will be used to separate the list.)


3 Answers

Array.prototype.join() is what you're looking for:

arr.join(',');

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/join


var arr = ['Hi', 'I', 'am', 'a', 'comma', 'separated', 'list'];

arr.join(',');  // === "Hi,I,am,a,comma,separated,list" 
like image 178
jAndy Avatar answered Oct 18 '22 23:10

jAndy


Use the join method:

arr.join(',');
like image 2
mbq Avatar answered Oct 18 '22 23:10

mbq


you have to use

var joinedstr = myarray.join(',');

like image 2
Oliver M Grech Avatar answered Oct 19 '22 00:10

Oliver M Grech