Convert an array to string

如何将此输出转换为字符串?

List<string> Client = new List<string>();
foreach (string listitem in lbClients.SelectedItems)
{
Client.Add(listitem);
}
288860 次浏览

您可以使用以下方法加入您的数组:

string.Join(",", Client);

Then you can output anyway you want. You can change the comma to what ever you want, a space, a pipe, or whatever.

你可能想要类似这样的超载字符串。加入:

String.Join<T> Method (String, IEnumerable<T>)

文件:

http://msdn.microsoft.com/en-us/library/dd992421.aspx

在你的例子中,你会使用

String.Join("", Client);

我的建议是:

using System.Linq;


string myStringOutput = String.Join(",", myArray.Select(p => p.ToString()).ToArray());

参考文献: Https://coderwall.com/p/oea7uq/convert-simple-int-array-to-string-c

你可以这样写:

        string[] arr = { "Miami", "Berlin", "Hamburg"};


string s = string.Join(" ", arr);