Click here to Skip to main content
15,880,725 members
Articles / General Programming / String
Alternative
Tip/Trick

String concatenation using LINQ to create a CSV/PSV string

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Jun 2011CPOL 19.6K   9
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)


Written By
Architect SunHotels
Spain Spain
I Received a Bachelor's Degree in Computer Science at the Mathematics and Computer Science Faculty, University of Havana, Cuba.

I mainly work in web applications using C# and some Javascript. Some very few times do some Java.

Comments and Discussions

 
GeneralConsider escaping the separator character within 'part' befo... Pin
TrevorJF5-Jul-11 3:31
TrevorJF5-Jul-11 3:31 
GeneralRe: Separator is a string, so it's already "escaped". string.Joi... Pin
Erich Ledesma6-Jul-11 8:55
Erich Ledesma6-Jul-11 8:55 
GeneralReason for my vote of 5 Good, Simpler and easy to implement ... Pin
All Time Programming4-Jul-11 20:57
All Time Programming4-Jul-11 20:57 
GeneralI agree :) Pin
Mohammad A Rahman28-Jun-11 3:01
Mohammad A Rahman28-Jun-11 3:01 
GeneralReason for my vote of 5 Reason of my vote 5: I like the rhyt... Pin
Mohammad A Rahman28-Jun-11 2:53
Mohammad A Rahman28-Jun-11 2:53 
GeneralRe: Thanx mate. The idea is always trying to make it as simple a... Pin
Erich Ledesma28-Jun-11 2:57
Erich Ledesma28-Jun-11 2:57 
GeneralI agree, it could be generic, and would be usefull. But in m... Pin
Erich Ledesma28-Jun-11 2:45
Erich Ledesma28-Jun-11 2:45 
GeneralNice one :) Pin
Mohammad A Rahman28-Jun-11 2:45
Mohammad A Rahman28-Jun-11 2:45 
GeneralWhy only for the strings? Could be a generic T as Append acc... Pin
Mario Majčica28-Jun-11 2:34
professionalMario Majčica28-Jun-11 2:34 
Why only for the strings? Could be a generic T as Append accepts an object and then tries to invoke ToString() method.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.