convert string array to string

后端 未结 9 2180
野性不改
野性不改 2020-12-02 10:59

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

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

I wou

9条回答
  •  死守一世寂寞
    2020-12-02 11:32

    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);
    

提交回复
热议问题