C# List string to string with delimiter
You can use String.Join
. If you have a List<string>
then you can call ToArray
first:
List<string> names = new List<string>() { "John", "Anna", "Monica" };
var result = String.Join(", ", names.ToArray());
In .NET 4 you don't need the ToArray
anymore, since there is an overload of String.Join
that takes an IEnumerable<string>
.
Results:
John, Anna, Monica
How to split() a delimited string to a List String
string.Split()
returns an array - you can convert it to a list using ToList()
:
listStrLineElements = line.Split(',').ToList();
Note that you need to import System.Linq
to access the .ToList()
function.
Convert a list of strings to a single string
string Something = string.Join(",", MyList);
Convert a list to a string in C#
Maybe you are trying to do
string combinedString = string.Join( ",", myList.ToArray() );
You can replace "," with what you want to split the elements in the list by.
Edit: As mentioned in the comments you could also do
string combinedString = string.Join( ",", myList);
Reference:
Join<T>(String, IEnumerable<T>)
Concatenates the members of a collection, using the specified separator between each member.
Convert a list into a comma-separated string
Enjoy!
Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));
First Parameter: ","
Second Parameter: new List<uint> { 1, 2, 3, 4, 5 })
String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.
Creating a comma separated list from IList string or IEnumerable string
.NET 4+
IList<string> strings = new List<string>{"1","2","testing"};
string joined = string.Join(",", strings);
Detail & Pre .Net 4.0 Solutions
IEnumerable<string>
can be converted into a string array very easily with LINQ (.NET 3.5):
IEnumerable<string> strings = ...;
string[] array = strings.ToArray();
It's easy enough to write the equivalent helper method if you need to:
public static T[] ToArray(IEnumerable<T> source)
{
return new List<T>(source).ToArray();
}
Then call it like this:
IEnumerable<string> strings = ...;
string[] array = Helpers.ToArray(strings);
You can then call string.Join
. Of course, you don't have to use a helper method:
// C# 3 and .NET 3.5 way:
string joined = string.Join(",", strings.ToArray());
// C# 2 and .NET 2.0 way:
string joined = string.Join(",", new List<string>(strings).ToArray());
The latter is a bit of a mouthful though :)
This is likely to be the simplest way to do it, and quite performant as well - there are other questions about exactly what the performance is like, including (but not limited to) this one.
As of .NET 4.0, there are more overloads available in string.Join
, so you can actually just write:
string joined = string.Join(",", strings);
Much simpler :)
Join List List String into a single string with delimiter
You may use the Select
LINQ method to form the inner joined string enclosed in parenthesis and then join the resulted IEnumerable<string>
:
string.Join(", ", listOfListOfvalues.Select(l => "(" + string.Join(", ", l) + ")"));
Full example:
List<List<string>> listOfListOfvalues = new List<List<string>>();
listOfListOfvalues.Add(new List<string>() { "a", "b", "c", "d" });
listOfListOfvalues.Add(new List<string>() { "A", "B", "C" });
listOfListOfvalues.Add(new List<string>() { "1", "2", "3", "4", "5" });
string joined =
string.Join(", ", listOfListOfvalues.Select(l => "(" + string.Join(", ", l) + ")"));
Console.WriteLine(joined);
// Prints: (a, b, c, d), (A, B, C), (1, 2, 3, 4, 5)
Convert `List string ` to comma-separated string
In .NET 4 you don't need the ToArray()
call - string.Join
is overloaded to accept IEnumerable<T>
or just IEnumerable<string>
.
There are potentially more efficient ways of doing it before .NET 4, but do you really need them? Is this actually a bottleneck in your code?
You could iterate over the list, work out the final size, allocate a StringBuilder
of exactly the right size, then do the join yourself. That would avoid the extra array being built for little reason - but it wouldn't save much time and it would be a lot more code.
List long to comma delimited string in C#
string.Join is your friend...
var list = new List<long> {1, 2, 3, 4};
var commaSeparated = string.Join(",", list);
Convert a delimited string to a List of KeyValuePair string,string in C#
You have to do some tricky stuff with splitting your string. Basically, you need to split by your main delimiter (';'
) so you end up with: key1=value1
, key2=value2
, key3=value3
. Then, you split each of those by the secondary delimiter ('='
). Select
that into a new key value pair of strings. It should look similar to this:
var str = "key1=value1;key2=value2;key3=value3";
List<KeyValuePair<string, string>> kvp = str.Split(';')
.Select(x => new KeyValuePair<string, string>
(x.Split('=')[0], x.Split('=')[1]))
.ToList();
EDIT:
As suggested in comment by Joel Lucsy, you can alter the select to only split once:
.Select(x => { var p = s.Split('='); return new KeyValuePair<string,string>(p[0],p[1]); })
Related Topics
Multiple Types Were Found That Match the Controller Named 'Home'
Addbusinessdays and Getbusinessdays
Showing a Windows Form on a Secondary Monitor
Why Does Calling a Method in My Derived Class Call the Base Class Method
Display Image from Database in Asp MVC
How to Set the Timeout for a Tcpclient
Unique Random String Generation
How to Handle Dependency Injection in a Wpf/Mvvm Application
Memory Address of an Object in C#
Pass-Through Mouse Events to Parent Control
Why Can't We Change Access Modifier While Overriding Methods in C#
Is Securestring Ever Practical in a C# Application
How to Format a Number into a String with Leading Zeros