HomeCSharpC# Tips and Tricks #13 – Create Comma seperated string from a List.

C# Tips and Tricks #13 – Create Comma seperated string from a List.

Assume that you have a List of string and you wish you to get the items of the list into a comma separated string , you will be able to it easily do with the help of string.join method.

How to generate a comma seperated string from a list in C# ?

Assume that the list contains three items Senthil , Kumar , CredAbility as shown below.

IList&lt;string&gt; lst = new List<string>{"Senthil","Kumar","CredAbility"};

To get the comma seperated string of all the items in the list , use the string.Join as shown below.

string commaseperatedstring = string.Join(",", lst);
image

Here’s the full code snippet that is used in this example.

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<string> lst = new List<string> { "Senthil", "Kumar", "CredAbility" };
            string commaseperatedstring = string.Join(",", lst);
            Console.WriteLine(commaseperatedstring);

            Console.ReadLine();
        }
    }
}
    

Leave a Reply

You May Also Like

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...