Rob
Rob

Reputation: 1575

Convert an array to string

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

Answers (4)

Arefe
Arefe

Reputation: 12397

You can write like this:

string[] arr = { "Miami", "Berlin", "Hamburg"};
string s = string.Join(" ", arr);

Upvotes: 11

Cleber Pessoal
Cleber Pessoal

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

adv12
adv12

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

CodeLikeBeaker
CodeLikeBeaker

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

Related Questions