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);
}
3

4 Answers

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.

0

You probably want something like this overload of String.Join:

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

Docs:

In your example, you'd use

String.Join("", Client);

My suggestion:

using System.Linq;

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

reference:

2

You can write like this:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest