Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / string

String concatenation using LINQ to create a CSV/PSV string

5.00/5 (2 votes)
29 Jun 2011CPOL 20.8K  
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 strings.

C#
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();
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)