C#: string[] to delimited string. Is there a one-liner?

20,636

Solution 1

For arrays, you can use:

string.Join(", ", strArray);

Personally, I use an extension method that I can apply to enumerable collections of all types:

public static string Flatten(this IEnumerable elems, string separator)
{
    if (elems == null)
    {
        return null;
    }

    StringBuilder sb = new StringBuilder();
    foreach (object elem in elems)
    {
        if (sb.Length > 0)
        {
            sb.Append(separator);
        }

        sb.Append(elem);
    }

    return sb.ToString();
}

...Which I use like so:

strArray.Flatten(", ");

Solution 2

You can use the static String.Join method:

String strNew = String.Join(chDelimiter, strArray);


EDIT: In response to comment: Based on your comment, you can take several arrays, concatenate them together, and then join the entire resulting array. You can do this by using the IEnumerable extension method Concat. Here's an example:

//define my two arrays...
string[] strArray = { "Hi", "how", "are", "you" };
string[] strArray2 = { "Hola", "como", "esta", "usted" };

//Concatenate the two arrays together (forming a third array) and then call join on it...
string strNew = String.Join(",", strArray.Concat(strArray2));

Hope this helps!

Solution 3

Have a look at String.Join().

Your sample must look like this :

        string delimiter = ","
        string[] strArray = { "Hi", "how", "are", "you" };
        string strNew = String.Join(delimiter, strArray);

Solution 4

Use String.Join

string[] strArray = {"Hi", "how", "are", "you"};
string strNew = String.Join("," strArray);

Solution 5

in this case, String.Join() is probably the easiest way to go, you can equally use LINQ though

var comSeparatedStrings = strings.Aggregate((acc, item) => acc + ", " + item);
Share:
20,636

Related videos on Youtube

XstreamINsanity
Author by

XstreamINsanity

Graduated from DeVry University in 2006 with a BS in Computer Engineering Technology. Hired directly out of college and worked for that company for just over three years. Moved to a new company after the previous one was bought out and we were given the ultimatum to move or find a new job. I program in C#, VB.Net, ASP.NET (including MVC2), and whatever other code I'm given the opportunity to work with.

Updated on August 25, 2020

Comments

  • XstreamINsanity
    XstreamINsanity over 3 years

    What I'd prefer is something like:

    string[] strArray = {"Hi", "how", "are", "you"};
    string strNew = strArray.Delimit(chDelimiter);
    

    However, there is no such function. I've looked over MSDN and nothing looked to me as a function that would perform the same action. I looked at StringBuilder, and again, nothing stood out to me. Does anyone know of a not to extremely complicated one liner to make an array a delimited string. Thanks for your guys' help.

    UPDATE: Wow, lol, my bad. I kept looking at the .Join on the array itself and it was bugging the hell out of me. I didn't even look at String.Join. Thanks guys. Once it allows me to accept I shall. Preciate the help.

    • Hamish Grubijan
      Hamish Grubijan over 13 years
      Something like String.Join(chDelimiter, array); You might have to turn char into a string first. There is also a generic version. I suspect that LINQ can do this in an elegant way as well.
    • XstreamINsanity
      XstreamINsanity over 13 years
      Yeah, I kept looking at the wrong join. Thanks.
  • XstreamINsanity
    XstreamINsanity over 13 years
    Nice function. My main purpose is we have a delimited file, put it into a program for editing with SQL Lite as the storing method, and then they want to sent it back out to a delimited file. Once it lets me accept I shall. Thanks.
  • XstreamINsanity
    XstreamINsanity over 13 years
    What bothers me is that if something is a string[], then when I do .Join, I should still have that option available. I should have the option to join arrays and the option to JOIN that array. :)
  • Danko Durbić
    Danko Durbić over 13 years
    In NET4.0, there is a string.Join overload you can use with any IEnumerable collection, msdn.microsoft.com/en-us/library/dd992421.aspx
  • Ed James
    Ed James over 13 years
    Aggregate is much slower though, due to not using a StringBuilder behind the scenes (iirc).
  • kbrimington
    kbrimington over 13 years
    @Danko: Nice! As I move my applications to .NET 4, I will definitely favor the method you suggested. Thanks!
  • XstreamINsanity
    XstreamINsanity over 13 years
    Yeah, I saw that. I didn't necessarily mean that I need it, I'm saying that when you have a string[] and you follow the variable with .Join (strArray.Join( ), you don't get the Join(string delimiter, string[] strArray) option, you get some other join options. I think the string[] should also have the .Join(string delimiter, string[] strArray) option, but it would be .Join(string delimiter) since it already is an array. Did I make that clear because I confused myself. :)
  • Andrew Hill
    Andrew Hill almost 8 years
    this approach also allows you to append a line separator also, with an extra parameter; the .Join approach requires an extra concat for the eol "\n"
  • Kiquenet
    Kiquenet about 6 years
    Performance about string.Join vs strArray.Flatten ?