Linq: join the results in a IEnumerable<string > to a single string
how do i join the results in a IEnumerable to a single string? the IEnumerable contains 20 single letters, and i want it to combine it to a single string.
And out of curiousity: how would i join it with a separator, for example if the IEnumerable contains the strings a b c d e how can开发者_JS百科 i join it to a,b,c,d,e?
Michel
Try this:
IEnumerable<string> letters = new[] { "a", "b", "c", "d", "e" };
string separator = ", ";
string withSeparator = String.Join(separator, letters.ToArray());
string withoutSeparator = String.Join(String.Empty, letters.ToArray());
Also, with 4.0 .NET there's a new simpler overload available: String.Join Method (String, IEnumerable<String>)
so you can skip the ToArray()
call.
精彩评论