Click here to Skip to main content
12,621,492 members (33,754 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3.3K views
Posted

String concatenation using LINQ to create a CSV/PSV string

, 29 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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...
The idea was good, but I think we could probably do something like below:

public static string UsingStringJoin(IEnumerable<string> sList, string separator)
{
    return sList.Any() ? string.Join(separator, sList.ToArray()) : string.Empty;
}
 
public static string UsingStringBuilder(IEnumerable<string> sList, string separator)
{
    return sList.Any() ? BuildString(sList, seperator) : string.Empty;
}
 
public static string BuildString(IEnumerable<string> sList, string separator)
{
    StringBuilder builder = new StringBuilder();
    foreach (string item in sList)
        builder.Append(string.Concat(item, separator));
    string buildedString = builder.ToString();
    return buildedString.Remove(buildedString.Length - 1);
}

If we have few strings to merge, probably use string.Concat(...) or string.Join(...) or for larger set of strings StringBuilder. Certainly your idea is good, I just want to show an alternate way to do the thing.

License

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

Share

About the Author


You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161128.1 | Last Updated 29 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid