views:

201

answers:

5

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.

+16  A: 

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(", ");
kbrimington
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
In NET4.0, there is a `string.Join` overload you can use with any `IEnumerable` collection, http://msdn.microsoft.com/en-us/library/dd992421.aspx
Danko Durbić
@Danko: Nice! As I move my applications to .NET 4, I will definitely favor the method you suggested. Thanks!
kbrimington
+4  A: 

Use String.Join

string[] strArray = {"Hi", "how", "are", "you"};
string strNew = String.Join("," strArray);
Jim Schubert
Thanks, I kept looking at the wrong join, I appreciate it.
XstreamINsanity
+5  A: 

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!

David Hoerster
Thanks, I kept looking at the wrong join, I appreciate it.
XstreamINsanity
No problem. Yeah, there are a few out there. :)
David Hoerster
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. :)
XstreamINsanity
See my update above -- I think this does what you're looking for.
David Hoerster
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. :)
XstreamINsanity
I'm a little confused, too. :) Makes sense. I wasn't sure if you were looking for it or not, but I thought I'd add it in anyway.
David Hoerster
+4  A: 

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);
Incognito
Thanks, I kept looking at the wrong join, I appreciate it.
XstreamINsanity
+1  A: 

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);
theburningmonk
Thanks for the info.
XstreamINsanity
Aggregate is much slower though, due to not using a StringBuilder behind the scenes (iirc).
Ed Woodcock