The shortest method to convert an array to a string in C # / LINQ

Closed as an exact duplicate of this question .

I have an array / list of elements. I want to convert it to a string separated by a custom separator. For example:

[1,2,3,4,5] => "1,2,3,4,5" 

What is the shortest / best way to do this in C #?

I always did this by looping through the list and checking to see if the current item is not the last before adding a separator.

 for(int i=0; i<arr.Length; ++i) { str += arr[i].ToString(); if(i<arr.Length) str += ","; } 

Is there a LINQ function that can help me write less code?

+43
arrays c # linq
Dec 19 '08 at 11:17
source share
2 answers
 String.Join(",", arr.Select(p=>p.ToString()).ToArray()) 
+133
Dec 19 '08 at 11:19
source share
 String.Join(",", array.Select(o => o.ToString()).ToArray()); 
+35
Dec 19 '08 at 11:19
source share



All Articles