Tip-Convert array to Comma delimited string

I was needed to convert an string array into the comma delimited string and the old way to do that is too with for loop. But I was sure that there should be some ready made function that will do this very easily.

After doing some research I have found string.Join. With the help of this we can easily an array into the comma delimited string.

String.join have two arguments one is separator and other one is either array or enumerable.

Following is a code for that.

namespace ConsoleApplication3
{
    class Program
    {
        static void Main() {
            string[] name = {"Jalpesh", "Vishal", "Tushar", "Gaurang"};
            string commnaDelmietedName = string.Join(",", name);
            System.Console.WriteLine(commnaDelmietedName);
        }
    }
}

Let’s run that example and here is the output as expected.

image

That’s it. Hope you like it. Stay tuned for more..

Original Blog: http://www.dotnetjalps.com/2013/01/Tip-Convert-array-to-Comma-delimited-string.html

Published Sunday, January 27, 2013 2:24 PM by Jalpesh P. Vadgama
Filed under: ,

Comments

# re: Tip-Convert array to Comma delimited string

Tuesday, January 29, 2013 8:31 AM by Richarles Gollet

OH. MY. GOD.

To do this I was looping over all the characters of all the strings and inserting commas and then I had to loop at the end to remove last commas and it was very complicated.