Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<int> to delimited string list [duplicate]

Tags:

c#

Possible Duplicate:
most elegant way to return a string from List<int>

I'm not sure the easiest way to do this. I simply want to add a ; between each value and spit it out as one string. I don't see that you can do this with ToString(). I'd have to loop through and create a stringbuilder and append & add a ";".

like image 920
PositiveGuy Avatar asked Jun 28 '10 19:06

PositiveGuy


2 Answers

string.Join(";", myList.ToArray()); 
like image 21
Jamie Ide Avatar answered Sep 23 '22 06:09

Jamie Ide


UPDATED to use List<int> instead of List<string>

Use string.Join:

List<int> data = ..; var result = string.Join(";", data); // (.NET 4.0+) var result = string.Join(";", data.Select(x => x.ToString()).ToArray()); // (.NET 3.5) 
like image 95
Stephen Cleary Avatar answered Sep 22 '22 06:09

Stephen Cleary