224

I would like to convert a string array to a single string.

string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";

I would like to have something like "Hello World!"

John Saunders
  • 160,644
  • 26
  • 247
  • 397

9 Answers9

394
string[] test = new string[2];

test[0] = "Hello ";
test[1] = "World!";

string.Join("", test);
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Dave Ward
  • 59,815
  • 13
  • 117
  • 134
79

A slightly faster option than using the already mentioned use of the Join() method is the Concat() method. It doesn't require an empty delimiter parameter as Join() does. Example:

string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";

string result = String.Concat(test);

hence it is likely faster.

Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
  • 1
    `Concat` is better, in inner code of `Join`, it will append separator after each item. But `Concat` not have such codes, it is simpler and more direct than `Join`. This is the fittst answer. – Mr. Squirrel.Downy May 19 '20 at 03:38
29

A simple string.Concat() is what you need.

string[] test = new string[2];

test[0] = "Hello ";
test[1] = "World!";

string result = string.Concat(test);

If you also need to add a seperator (space, comma etc) then, string.Join() should be used.

string[] test = new string[2];

test[0] = "Red";
test[1] = "Blue";

string result = string.Join(",", test);

If you have to perform this on a string array with hundereds of elements than string.Join() is better by performace point of view. Just give a "" (blank) argument as seperator. StringBuilder can also be used for sake of performance, but it will make code a bit longer.

ePandit
  • 2,905
  • 2
  • 24
  • 15
24

Try:

String.Join("", test);

which should return a string joining the two elements together. "" indicates that you want the strings joined together without any separators.

davidg
  • 5,868
  • 2
  • 33
  • 51
  • Wouldn't that make "HelloWorld!" without a space between the words? – jamesmortensen Jan 30 '11 at 06:33
  • 1
    @jmort253 - The original string `"Hello "` in the question already had a trailing space. If the space was not already there, then you would be correct, and using `" "` as a separator would make more sense. – davidg Jan 30 '11 at 06:36
8

In the accepted answer, String.Join isn't best practice per its usage. String.Concat should have be used since OP included a trailing space in the first item: "Hello " (instead of using a null delimiter).

However, since OP asked for the result "Hello World!", String.Join is still the appropriate method, but the trailing whitespace should be moved to the delimiter instead.

// string[] test = new string[2];

// test[0] = "Hello ";
// test[1] = "World!";

string[] test = { "Hello", "World" }; // Alternative array creation syntax 
string result = String.Join(" ", test);
Vilename
  • 107
  • 1
  • 1
7

Aggregate can also be used for same.

string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string joinedString = test.Aggregate((prev, current) => prev + " " + current);
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 1
    This is extremely wasteful method to implement `String.Join` due to multiple extra strings constructed. Please read https://stackoverflow.com/questions/217805/using-linq-to-concatenate-strings for proper variant of `Aggregate`. – Alexei Levenkov Dec 03 '18 at 04:56
3
    string ConvertStringArrayToString(string[] array)
    {
        //
        // Concatenate all the elements into a StringBuilder.
        //
        StringBuilder strinbuilder = new StringBuilder();
        foreach (string value in array)
        {
            strinbuilder.Append(value);
            strinbuilder.Append(' ');
        }
        return strinbuilder.ToString();
    }
Kiquenet
  • 14,494
  • 35
  • 148
  • 243
programmer
  • 878
  • 5
  • 8
  • 4
    This is just manually implementing `string.Join(" ", array)` (except yours adds a trailing space). Why not just use the one in the library? – davidg Jan 30 '11 at 06:28
  • 2
    @davidg - Ashwini is probably new. Learning to use existing tools and getting over the "I must do it myself" mentality of college takes some getting used to. It did for me. Eventually, people wonder why their colleagues are able to code circles around them, and then they see the value in code libraries and reuse. – jamesmortensen Jan 30 '11 at 06:41
  • whatever i remembered i tried to help that's all..if anybody has a better solution you are always free to post it.. @jmort:it's been 4 years since i'm working...i don't have the mentality of wht u r talking abt...anybody is not perfect...over the years of experience you learn ..hope u don't deny this fact.. – programmer Jan 30 '11 at 06:57
  • @Ashwini - Wasn't trying to offend you. Just answering @davidg's question based on my experience working with different engineers. – jamesmortensen Jan 30 '11 at 19:42
  • 2
    Also, the concatenated string will have an extra space tacked onto the end of it. So this doesn't fully meet the question. – jamesmortensen Jan 31 '11 at 00:38
0

I used this way to make my project faster:

RichTextBox rcbCatalyst = new RichTextBox()
{
    Lines = arrayString
};
string text = rcbCatalyst.Text;
rcbCatalyst.Dispose();

return text;

RichTextBox.Text will automatically convert your array to a multiline string!

Bamdad
  • 726
  • 1
  • 11
  • 28
-9

Like this:

string str= test[0]+test[1];

You can also use a loop:

for(int i=0; i<2; i++)
    str += test[i];
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
Canaan
  • 1
  • 2