Home
C#
Convert List to String
This page was last reviewed on Dec 15, 2021.
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.
Shows a string list
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.
IEnumerable
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.
string.Join
Info In previous versions of .NET, we had to call ToArray on a List before using Join. In older programs this is still required.
Shows a string list
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.
StringBuilder
Detail The example has a final delimiter on the end. This is not present in code that uses string.Join—it can be inconvenient.
Detail Sometimes, it is good to remove the end delimiter with TrimEnd. Other times it is best left alone.
String TrimEnd, TrimStart
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.
Detail StringBuilder is fast for most programs. More speed could be acquired by using a char[] and then converting to a string.
char Array
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.
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.
List
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 15, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.