List
, string
Think of a sentence. It contains some words. We could represent a sentence as a single string
—one with spaces. But a list of strings (of words) is sometimes better.
We can convert our string
into a List
of smaller strings. For the reverse, we can convert our List
into a string
. A List
can be joined directly as it is an IEnumerable
type.
Often the best way to convert a List
of strings into an array is the string.Join
method. This is built into the .NET Framework, so we do not need to write any custom code.
List
of strings by invoking the List
constructor, and then calling Add()
3 times.string.Join
method to combine a List
of strings into one string
. The output can be used as a CSV record.ToArray
on a List
before using Join
. In older programs this is still required.using System; using System.Collections.Generic; class Program { static void Main() { // Part 1: create list of strings. List<string> animals = new List<string>(); animals.Add("bird"); animals.Add("bee"); animals.Add("cat"); // Part 2: use string.Join and pass the list as an argument. string result = string.Join(",", animals); Console.WriteLine($"RESULT: {result}"); } }RESULT: bird,bee,cat
StringBuilder
Here we use the StringBuilder
class
to convert a List
to a single string
. We can convert a List
of any object type into a string
this way.
string.Join
—it can be inconvenient.TrimEnd
. Other times it is best left alone.using System; using System.Collections.Generic; using System.Text; class Program { static void Main() { List<string> cats = new List<string>(); cats.Add("Devon Rex"); cats.Add("Manx"); cats.Add("Munchkin"); cats.Add("American Curl"); cats.Add("German Rex"); StringBuilder builder = new StringBuilder(); // Loop through all strings. foreach (string cat in cats) { // Append string to StringBuilder. builder.Append(cat).Append("|"); } // Get string from StringBuilder. string result = builder.ToString(); Console.WriteLine(result); } }Devon Rex|Manx|Munchkin|American Curl|German Rex|
Int
List
Here we convert a List
of ints into a single string
. The StringBuilder
's Append
method receives a variety of types. We can simply pass it the int
.
Append()
will handle the int
on its own. It will convert it to a string
and append it.StringBuilder
is fast for most programs. More speed could be acquired by using a char[]
and then converting to a string
.using System; using System.Collections.Generic; using System.Text; class Program { static void Main() { List<int> safePrimes = new List<int>(); safePrimes.Add(5); safePrimes.Add(7); safePrimes.Add(11); safePrimes.Add(23); StringBuilder builder = new StringBuilder(); foreach (int safePrime in safePrimes) { // Append each int to the StringBuilder overload. builder.Append(safePrime).Append(" "); } string result = builder.ToString(); Console.WriteLine(result); } }5 7 11 23
string
Finally, we get a List
of strings from a string
in CSV format. This requires the Split
method. If you require per-item conversion, loop over the string
array returned by Split
.
using System; using System.Collections.Generic; class Program { static void Main() { string csv = "one,two,three"; string[] parts = csv.Split(','); // Use List constructor. List<string> list = new List<string>(parts); foreach (string item in list) { Console.WriteLine(item); } } }one two three
We converted Lists and strings using the string.Join
methods and the StringBuilder
approach. The List
is easily concatenated and stored in a database or file with these methods.