我想将一个字符串数组转换为单个字符串。
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
我想要一些像“Hello World!”
我想将一个字符串数组转换为单个字符串。
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
我想要一些像“Hello World!”
当前回答
我用这种方法让我的项目更快:
RichTextBox rcbCatalyst = new RichTextBox()
{
Lines = arrayString
};
string text = rcbCatalyst.Text;
rcbCatalyst.Dispose();
return text;
RichTextBox。文本将自动将您的数组转换为多行字符串!
其他回答
比使用已经提到的Join()方法稍微快一点的选项是Concat()方法。它不像Join()那样需要空分隔符参数。例子:
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string result = String.Concat(test);
因此,它可能更快。
string[] test = new string[2];
test[0] = "Hello ";
test[1] = "World!";
string.Join("", test);
我用这种方法让我的项目更快:
RichTextBox rcbCatalyst = new RichTextBox()
{
Lines = arrayString
};
string text = rcbCatalyst.Text;
rcbCatalyst.Dispose();
return text;
RichTextBox。文本将自动将您的数组转换为多行字符串!
Try:
String.Join("", test);
它应该返回一个将两个元素连接在一起的字符串。""表示您希望将字符串连接在一起而不使用任何分隔符。
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();
}