Click here to Skip to main content
13,148,387 members (54,933 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.1K views
Posted 30 Jun 2011

String concatenation using LINQ to create a CSV/PSV string

, 30 Jun 2011
Rate this:
Please Sign up or sign in to vote.
You can use the Aggregate method with a StringBuilder. I've modified Eric's alternative in order to save a bit of code. Only one return statement is needed as an empty StringBuilder returns an empty string.public static string Join(this IEnumerable parts, string separator) { ...
You can use the Aggregate method with a StringBuilder. I've modified Eric's alternative in order to save a bit of code. Only one return statement is needed as an empty StringBuilder returns an empty string.
public static string Join(this IEnumerable<string> parts, string separator)
       {
           var builder = new StringBuilder();
           if (parts.Any())
           {
               builder.Append(parts.First());
               parts.Skip(1).Aggregate(builder, (sb, s) => sb.Append(separator).Append(s));
           }
           return builder.ToString();
       }

License

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

Share

About the Author

George Swan
Student
Wales Wales
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 30 Jun 2011
Article Copyright 2011 by George Swan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid