Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert an array of Ints to a comma separated string [duplicate]

I know that if I want to convert an array of Ints to a String, I do this:

[0,1,1,0].map{"\($0)"}.reduce(""){$0+$1} 

but I cannot figure out how would I convert an array of Ints to a comma separated String

like image 514
Lauren Eccles Avatar asked May 04 '17 05:05

Lauren Eccles


People also ask

How to create a comma-separated 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.

How to make comma-separated string from array in Java?

The simplest way to convert an array to comma separated String is to create a StringBuilder, iterate through the array, and add each element of the array into StringBuilder after appending the comma.

How to convert array to comma-separated string?

To convert an array to a comma-separated string, call the join() method on the array, passing it a string containing a comma as a parameter. The join method returns a string containing all array elements joined by the provided separator.

How do you get a comma-separated string from an array in C?

How to get a comma separated string from an array in C#? We can get a comma-separated string from an array using String. Join() method. In the same way, we can get a comma-separated string from the integer array.


1 Answers

Well you can do it like :

let formattedArray = ([0,1,1,0].map{String($0)}).joined(separator: ",") 
like image 84
ankit Avatar answered Sep 21 '22 09:09

ankit