Convert a list into a comma-separated string

355,190

Solution 1

Enjoy!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

First Parameter: ","
Second Parameter: new List<uint> { 1, 2, 3, 4, 5 })

String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.

Solution 2

You can use String.Join method to combine items:

var str = String.Join(",", lst);

Solution 3

Using String.Join:

string.Join<string>(",", lst);

Using LINQ aggregation:

lst .Aggregate((a, x) => a + "," + x);

Solution 4

If you have a collection of ints:

List<int> customerIds= new List<int>() { 1,2,3,3,4,5,6,7,8,9 };  

You can use string.Join to get a string:

var result = String.Join(",", customerIds);

Enjoy!

Solution 5

Follow this:

List<string> name = new List<string>();
name.Add("Latif");
name.Add("Ram");
name.Add("Adam");
string nameOfString = (string.Join(",", name.Select(x => x.ToString()).ToArray()));
Share:
355,190

Related videos on Youtube

skjcyber
Author by

skjcyber

My name is Soumitra Kumar Jana and I am currently working as a Cloud Architect specializing in Microsoft Azure Cloud. My area of expertise includes - Cloud Platform & Infrastructure | Automation | DevOps | Azure Security | Infrastructure-as-a-Code | .NET Development A Software Developer having sound knowledge of all aspects of SDLC - Requirement Gathering, Development, Automated Testing, Build and Automated Deployment. Experienced in: C#, PowerShell, Microsoft Azure, SQL Server, REST API, Azure DevOps, Terraform, Agile Methodologies, Automation Testing, Virtualization (Application, Desktop, Hyper, User State) etc. The three chief virtues of a programmer are: Laziness, Impatience and Hubris - Larry Wall, Creator of the Perl programming language.

Updated on June 11, 2021

Comments

  • skjcyber
    skjcyber over 1 year

    My code is as below:

    public void ReadListItem()
    {
         List<uint> lst = new List<uint>() { 1, 2, 3, 4, 5 };
         string str = string.Empty;
         foreach (var item in lst)
             str = str + item + ",";
         str = str.Remove(str.Length - 1);
         Console.WriteLine(str);
    }
    

    Output: 1,2,3,4,5

    What is the most simple way to convert the List<uint> into a comma-separated string?

  • Anton
    Anton almost 9 years
    In .NET 3.5 and below you have to explicitly convert your list to array with lst.ToArray(), as there is no direct overload there yet.
  • Hari almost 8 years
    I have list of type int32. when I use aggregate function you mentioned, it says "Cannot convert lambda expression to delegate type 'System.Func<int,int,int>' because some of the return types in the block are not implicitly convertible to the delegate return type " and "Cannot implicitly convert type 'string' to 'int'"
  • bets over 5 years
    @Hari You must convert it to string values before you Aggragate to string. So you can do something like this: list.Select(x => string.Format("{0}:{1}", x.Key, x.Value)).Aggregate((a, x) => a+ ", " + x);
  • Rani Radcliff
    Rani Radcliff about 3 years
    this does not work for me. It produces the name of the collection rather than the objects in the list.
  • Peter Mortensen
    Peter Mortensen over 1 year
    The question was about C#, not Java.
  • Peter Mortensen
    Peter Mortensen over 1 year
    This is essentially identical to the accepted answer from 2013.
  • Peter Mortensen
    Peter Mortensen over 1 year
    From what C# version is this valid?
  • Hans Kesting
    Hans Kesting over 1 year
    Is this Javascript? (the question is about C#)