Convert array of integers to comma-separated string
It's a simple question; I am a newbie in C#, how can I perform the following
- I want to convert an array of integers to a comma-separated string.
I have
int[] arr = new int[5] {1,2,3,4,5};
I want to convert it to one string
string => "1,2,3,4,5"
Solution 1:
var result = string.Join(",", arr);
This uses the following overload of string.Join
:
public static string Join<T>(string separator, IEnumerable<T> values);
Solution 2:
.NET 4
string.Join(",", arr)
.NET earlier
string.Join(",", Array.ConvertAll(arr, x => x.ToString()))
Solution 3:
int[] arr = new int[5] {1,2,3,4,5};
You can use Linq for it
String arrTostr = arr.Select(a => a.ToString()).Aggregate((i, j) => i + "," + j);