Concatenate all list content in one string in C#
C#ListC# 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);