Click here to Skip to main content
15,891,976 members
Articles / General Programming / String

String concatenation using LINQ to create a CSV/PSV string

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
26 Jun 2011CPOL 43.1K   1  
A tip to concatenate a set of strings using comma/pipe

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
29 Jun 2011Erich Ledesma
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...
Please Sign up or sign in to vote.
27 Jun 2011Mario Majčica
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) { ...
Please Sign up or sign in to vote.
29 Jun 2011Mohammad A Rahman
The idea was good, but I think we could probably do something like below:public static string UsingStringJoin(IEnumerable sList, string separator){ return sList.Any() ? string.Join(separator, sList.ToArray()) : string.Empty;}public static string...
Please Sign up or sign in to vote.
30 Jun 2011George Swan
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) { ...

License

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



Comments and Discussions