8

Let's say I have an array of strings:

string[] myStrings = new string[] { "First", "Second", "Third" };

I want to concatenate them so the output is:

First Second Third

I know I can concatenate them like this, but there'll be no space in between:

string output = String.Concat(myStrings.ToArray());

I can obviously do this in a loop, but I was hoping for a better way.

Is there a more succinct way to do what I want?

4

2 に答える 2

29

これを試して:

String output = String.Join(" ", myStrings);
于 2009-05-12T00:12:35.767 に答える
1
StringBuilder buf = new StringBuilder();
foreach(var s in myStrings)
  buf.Append(s).Append(" ");
var ss = buf.ToString().Trim();
于 2009-05-12T00:15:35.263 に答える