Click here to Skip to main content
15,867,308 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.
4.67/5 (2 votes)
27 Jun 2011CPOL 13K   5
Or even better:public static string Join(this IList list, string joinString) { StringBuilder result = new StringBuilder(); int listCount = list.Count; int listCountMinusOne = listCount - 1; if (list != null && listCount > 0) { ...
Or even better:

C#
public static string Join<T>(this IList<T> list, string joinString)
    {
        StringBuilder result = new StringBuilder();

        int listCount = list.Count;
        int listCountMinusOne = listCount - 1;

        if (list != null && listCount > 0)
        {
            if (listCount > 1)
            {
                for (var i = 0; i < listCount; i++)
                {
                    if (i != listCountMinusOne)
                    {
                        result.Append(list[i]);
                        result.Append(joinString);
                    }
                    else
                        result.Append(list[i]);
                }
            }
            else
                result.Append(list[0]);
        }

        return result.ToString();
    }

License

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


Written By
Software Developer (Senior)
Netherlands Netherlands
An accomplished software engineer specialized in object-oriented design and analysis on Microsoft .NET platform with extensive experience in the full life cycle of the software design process.
Experienced in agile software development via scrum and kanban frameworks supported by the TFS ALM environment and JIRA. In depth know how on all automation process leading to continuous integration, deployment and feedback.
Additionally, I have a strong hands-on experience on deploying and administering Microsoft Team Foundation Server (migrations, builds, deployment, branching strategies, etc.).

Comments and Discussions

 
GeneralRe: You are perfectly right. When I wrote this code down for the... Pin
Mario Majčica28-Jun-11 2:31
professionalMario Majčica28-Jun-11 2:31 
GeneralAnd what is the down of this implementation? Pin
Mario Majčica28-Jun-11 2:09
professionalMario Majčica28-Jun-11 2:09 
GeneralRe: You have literally reinvented the wheel from a well known ex... Pin
JV999928-Jun-11 2:23
professionalJV999928-Jun-11 2:23 
GeneralReason for my vote of 2 This is basically string.Join only t... Pin
JV999928-Jun-11 2:05
professionalJV999928-Jun-11 2:05 
GeneralThis is basically string.Join only then not on the string, b... Pin
JV999928-Jun-11 2:05
professionalJV999928-Jun-11 2:05 

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.