Reputation: 1575
How do I make this output to a string?
List<string> Client = new List<string>();
foreach (string listitem in lbClients.SelectedItems)
{
Client.Add(listitem);
}
Upvotes: 145
Views: 349041
Reputation: 12397
You can write like this:
string[] arr = { "Miami", "Berlin", "Hamburg"};
string s = string.Join(" ", arr);
Upvotes: 11
Reputation: 109
My suggestion:
using System.Linq;
string myStringOutput = String.Join(",", myArray.Select(p => p.ToString()).ToArray());
reference: https://coderwall.com/p/oea7uq/convert-simple-int-array-to-string-c
Upvotes: 10
Reputation: 8551
You probably want something like this overload of String.Join:
String.Join<T> Method (String, IEnumerable<T>)
Docs:
http://msdn.microsoft.com/en-us/library/dd992421.aspx
In your example, you'd use
String.Join("", Client);
Upvotes: 17
Reputation: 21312
You can join your array using the following:
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.
Upvotes: 306