Concatenate all list content in one string in C#

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

Answers


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

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

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

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.


Here is a simple solution with StringBuilder:

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

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

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.


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

Try this:

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

Need Your Help

Casting interfaces for deserialization in JSON.NET

c# .net json json.net

I am trying to set up a reader that will take in JSON objects from various websites (think information scraping) and translate them into C# objects. I am currently using JSON.NET for the deseriali...

Android: Remove ALL FragmentTransactions from the back stack

android android-fragments

I add a bunch of FragmentTransactions to the back stack in one Tab, and I want to clear them ALL from the back stack when the user selects a different Tab. I can't find a method to clear stuff off ...