Convert an array to string

C#ArraysString

C# Problem Overview


How do I make this output to a string?

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

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

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

Solution 4 - C#

You can write like this:

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobView Question on Stackoverflow
Solution 1 - C#CodeLikeBeakerView Answer on Stackoverflow
Solution 2 - C#adv12View Answer on Stackoverflow
Solution 3 - C#Cleber PessoalView Answer on Stackoverflow
Solution 4 - C#ArefeView Answer on Stackoverflow