C# Convert List to StringUse the string.Join method and StringBuilder to convert Lists and strings.
dot net perls

Convert 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.


First example. 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.

Part 1 We create a List of strings by invoking the List constructor, and then calling Add() 3 times.

Part 2 We use the string.Join method to combine a List of strings into one string. The output can be used as a CSV record.


Info In previous versions of .NET, we had to call ToArray on a List before using Join. In older programs this is still required.

C# program that converts List to string
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.


Final delimiter The example has a final delimiter on the end. This is not present in code that uses string.Join—it can be inconvenient.

TrimEnd Sometimes, it is good to remove the end delimiter with TrimEnd. Other times it is best left alone.

TrimEnd, TrimStart

C# program that uses List and StringBuilder
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.

And Append() will handle the int on its own. It will convert it to a string and append it.

Performance StringBuilder is fast for most programs. More speed could be acquired by using a char[] and then converting to a string.

Char Array

C# program that converts List types
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

CSV 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.

C# program that converts string to List
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

A summary. 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.


© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.