TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Convert List to String

These C# programs convert Lists and strings. They use string.Join and StringBuilder.

Convert list, string. A List can be converted to a string.

This is possible with the ToArray method on the List type. We can also convert a string into a List. The StringBuilder type helps with certain conversions, which are done with loops.

Example. We use the string.Join method to combine a List of strings into a single string. The output can be used as a CSV record. The tricky part here is to know that you can call the ToArray() method on your List before using Join.

Based on:

.NET 4

C# program that converts List

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
	List<string> dogs = new List<string>();
	dogs.Add("Aigi"); // Add string 1
	dogs.Add("Spitz"); // 2
	dogs.Add("Mastiff"); // 3
	dogs.Add("Finnish Spitz"); // 4
	dogs.Add("Briard"); // 5

	string dogCsv = string.Join(",", dogs.ToArray());
	Console.WriteLine(dogCsv);
    }
}

Output

Aigi,Spitz,Mastiff,Finnish Spitz,Briard

Example 2. Here we use the StringBuilder class to convert your List to a single string. Note that you can convert a List of any object type into a string this way. Please see the next section for an example of that.

StringBuilder Secrets

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>(); // Create new list of strings
	cats.Add("Devon Rex"); // Add string 1
	cats.Add("Manx"); // 2
	cats.Add("Munchkin"); // 3
	cats.Add("American Curl"); // 4
	cats.Add("German Rex"); // 5

	StringBuilder builder = new StringBuilder();
	foreach (string cat in cats) // Loop through all strings
	{
	    builder.Append(cat).Append("|"); // Append string to StringBuilder
	}
	string result = builder.ToString(); // Get string from StringBuilder
	Console.WriteLine(result);
    }
}

Output

Devon Rex|Manx|Munchkin|American Curl|German Rex|

The example above has a final delimiter on the end of the output. This is not present in the solution that uses string.Join, and it can be inconvenient. Sometimes, it is good to remove it with TrimEnd. Other times it is best left alone.

TrimEnd and TrimStart Methods

Example 3. Here we convert a List of ints into a single string. The StringBuilder's Append method receives a variety of different types, and here we can simply pass it the int and 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.

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>(); // Create list of ints
	safePrimes.Add(5); // Element 1
	safePrimes.Add(7); // Element 2
	safePrimes.Add(11); // Element 3
	safePrimes.Add(23); // Element 4

	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);
    }
}

Output

5 7 11 23

Example 4. Finally, we get a List of strings from a string in CSV format. This requires the Split method on string. If you require per-item conversion, you can 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"; // The input string
	string[] parts = csv.Split(','); // Call Split method
	List<string> list = new List<string>(parts); // Use List constructor
	foreach (string item in list)
	{
	    Console.WriteLine(item);
	}
    }
}

Output

one
two
three

Summary. We converted Lists and strings in the C# language, using the string.Join methods and the StringBuilder approach. The List is easily concatenated and stored in a database or file with these methods.

String Join Method


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf