String concatenation using LINQ to create a CSV/PSV string





5.00/5 (2 votes)
I rather like IEnumerable than IList, parameters should be as general as posible. I think it's really getting very fun up here. Here's my choice, just for strings.public static string Join(this IEnumerable parts, string separator){ if (! parts.Any()) return...
I rather like
IEnumerable<T>
than IList<T>
, parameters should be as general as posible. I think it's really getting very fun up here. Here's my choice, just for string
s.
public static string Join(this IEnumerable<string> parts, string separator)
{
if (! parts.Any())
return string.Empty;
var builder = new StringBuilder();
builder.Append(parts.First()) ;
foreach(var part in parts.Skip(1))
{
builder.Append(separator) ;
builder.Append(part);
}
return builder.ToString();
}