Click here to Skip to main content
11,486,673 members (69,689 online)
Click here to Skip to main content

Tagged as

String concatenation using LINQ to create a CSV/PSV string

, 28 Jun 2011 CPOL 3.8K
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Mario Majčica
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.).
Follow on   Twitter   LinkedIn

Comments and Discussions

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

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Jun 2011
Article Copyright 2011 by Mario Majčica
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid