我想将一个字符串数组转换为单个字符串。

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

我想要一些像“Hello World!”


当前回答

是这样的:

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

你也可以使用循环:

for(int i=0; i<2; i++)
    str += test[i];

其他回答

string[] test = new string[2];

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

string.Join("", test);

比使用已经提到的Join()方法稍微快一点的选项是Concat()方法。它不像Join()那样需要空分隔符参数。例子:

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

string result = String.Concat(test);

因此,它可能更快。

你所需要的就是一个简单的string.Concat()。

string[] test = new string[2];

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

string result = string.Concat(test);

如果你还需要添加分隔符(空格,逗号等),那么应该使用string.Join()。

string[] test = new string[2];

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

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

如果必须在包含数百个元素的字符串数组上执行此操作,那么从性能角度来看,string. join()更好。只需要给出一个“”(空白)参数作为分隔符。为了提高性能,也可以使用StringBuilder,但它会使代码变长。

是这样的:

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

你也可以使用循环:

for(int i=0; i<2; i++)
    str += test[i];

我用这种方法让我的项目更快:

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

return text;

RichTextBox。文本将自动将您的数组转换为多行字符串!