convert string array to string

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

    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!

    0 讨论(0)
  • 2020-12-02 11:31
        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();
        }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题