How to convert list to string with delimiter C#

In this post, we will learn about how to convert a list to a string with delimiter C#. We are using the LINQ aggregate() function,StringBuilder append() and Array.ConvertAll() to convert list to string with delimiter with examples.

How to convert list to string with delimiter


In this C# example, we are using Linq Aggregate() function to convert a list to a string with a delimiter. We have used a comma(,) delimiter,instead of comma(,) you can use semicolon(;),hyphen(/),underscore (_) as per need.

using System;
using System.Collections.Generic;
using System.Linq;

namespace list_to_string
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> langs = new List<string>() { "C#", "C++", "Rust" };
            string strlangs = langs.Aggregate((a, b) => a + ", " + b);
            Console.WriteLine(strlangs);
        }
    }
}

Output

C#, C++, Rust

2. How to convert list to string with delimiter


In this example, we will convert a list to a string with a delimiter(,) The first method we have used is to convert a list to a string using a string. The second way is to use the join() method by using the Array.ConvertAll() converts an array of one type to another type here converting each element of the array to a string and joining them with a comma(,) delimiter.

The third way we have used string.join() method along with distinct() and select that Distinct() will return distinct element of the list and select will convert each element of the list and convert into an array of strings.

using System;
using System.Collections.Generic;
using System.Linq;

namespace list_to_string
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> langs = new List<string>() { "C#", "C++", "Rust" };
            var strlangs = string.Join(",", langs); 
         
          //Second ways 
          var strlangs1 =  string.Join(",", Array.ConvertAll(langs.ToArray(), item => item.ToString()));

          //ways -3
       var strlangs2 =  string.Join(",", langs.Distinct().Select(x => x.ToString()).ToArray());
            
           
            Console.WriteLine(strlangs);
       
        }
    }
}

Output

C#, C++, Rust

3. How to convert list to string with delimiter


In this C# program, We have used StringBuilder to convert a list to a string and for each loop to iterate over the list and append to a string using the append() method. Let us understand with the below examples.

using System;
using System.Collections.Generic;
using System.Linq;

namespace list_to_string
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> langs = new List<string>() { "C#", "C++", "Rust" };
            var strlangs = new System.Text.StringBuilder(); 
            
            langs.ForEach(ele => strlangs.Append((strlangs.Length > 0 ?", " : "") + ele)); 
            
            Console.WriteLine(strlangs);
        }
    }
}

Output

C#, C++, Rust