Concatenate all list content in one string in C#

C#List

C# Problem Overview


How do I concatenate all content of a list in one string in C#?

C# Solutions


Solution 1 - C#

Searching for this:

List<string> list = new List<string>(); // { "This ", "is ", "your ", "string!"};
list.Add("This ");
list.Add("is ");
list.Add("your ");
list.Add("string!");

string concat = string.Join(" ", list.ToArray());

Solution 2 - C#

You can use the Aggregate function to concatenate all items of a list.

The following is the example to concatenate all the items of a list with comma ","

string s = list.Aggregate((i, j) => i + "," + j).ToString();

Solution 3 - C#

If you have some properties within your objects and want to do some more formatting there, then using LINQ,

var output = string.Join(" ", list.Select(t => t.Prop1 + " " + t.Prop2));

This will put a space in between each of the properties of your objects and a space in between each object as well.

Solution 4 - C#

If you need to transform your list elements while joining, you can use LINQ:

string.Join(", ", mylist.Select(z => MyMethod(z)));

Example:

IEnumerable<string> mylist = new List<string>() {"my", "cool", "list"};
string joined = string.Join(", ", mylist.Select(z => MyMethod(z)));


public string MyMethod(string arg)
{
    return arg.ToUpper();
}

Solution 5 - C#

Here is a simple solution with StringBuilder:

        var sb = new StringBuilder();
        strings.ForEach(s => sb.Append(s));
        var combinedList = sb.ToString();

Solution 6 - C#

Something like this:

    List<String> myStrings = new List<string>() { "string1", "string2", "string3" };
    StringBuilder concatenatedString = new StringBuilder();
    foreach (String myString in myStrings)
    {
        concatenatedString.Append(myString);
    }
    Console.WriteLine(concatenatedString.ToString());

Solution 7 - C#

public static string ToString<T>(this IEnumerable<T> source, string delimiter)
{
    string d = String.Empty;
    var result = new StringBuilder();
    foreach(T item in source)
    {
        result.Append(d).Append(item.ToString());
        d = delimiter;
    }
    return result.ToString();
}

And an overload for optional better string conversion of individual values:

public static string ToString<T>(this IEnumerable<T> source, string delimiter, Func<T, string> toString)
{
    string d = String.Empty;
    var result = new StringBuilder();
    foreach(T item in source)
    {
        result.Append(d).Append(toString(item));
        d = delimiter;
    }
    return result.ToString();
}

Solution 8 - C#

Here is a version that separates elements using two spaces implemented using LINQ:

var s = list.Aggregate(new StringBuilder(), (sb, current) => 
  sb.AppendFormat("{0}  ", current)).ToString();

The Aggregate method passes the StringBuilder (as sb) with the current element of the list (as current) to the lambda expression which can do anything it needs - for example append the current string from the list to the StringBuilder using some formatting specified in the lambda expression body.

Solution 9 - C#

Try this:

request.AllFacilityId = request.FacilityIdList.Aggregate((i, j) => i + "," + j);

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
QuestionkartalView Question on Stackoverflow
Solution 1 - C#SeckoView Answer on Stackoverflow
Solution 2 - C#Babuji GodemView Answer on Stackoverflow
Solution 3 - C#WaughWaughView Answer on Stackoverflow
Solution 4 - C#Chris WView Answer on Stackoverflow
Solution 5 - C#LVBenView Answer on Stackoverflow
Solution 6 - C#EmmanuelView Answer on Stackoverflow
Solution 7 - C#Joel CoehoornView Answer on Stackoverflow
Solution 8 - C#Tomas PetricekView Answer on Stackoverflow
Solution 9 - C#Prasad ShigwanView Answer on Stackoverflow