convert string array to string

后端 未结 9 2178
野性不改
野性不改 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:25

    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.

提交回复
热议问题