Convert Array To Comma Separated String C#

Convert Array To Comma Separated String C#


c# convert array to comma separated string

  you may want to convert array of string into commama separated single string.
you can use String.Join function as explained in below example.


Example

String.Join uses System as a namespace


Code


 using System;

namespace ConsoleAppication
{
    class Program
    {
        static void Main()
        {
           
          string []arrString = new string[] { "Success","Fail","ok","interactive","pass" };

          string commaSepString = string.Join(",", arrString);

            Console.WriteLine( commaSepString );
            Console.ReadLine();

        }

     
    }
}


Output
Success,Fail,ok,interactive,pass

Description

 String.Join concatenates the elements of array, using specified separator string provided by user.
you can even use pipe "|" operator insted of comma to join elements of an array.

Remarks

If the element is null, it uses String.empty as a string.

You may want to convert string into comma separated string at spaces.

Code

protected void Button1_Click(object sender, EventArgs e)

{

string str = "Error message 103 105 3125 5568 9806 9890 99233 Fail";

string []arrString = str.Split(" ".ToCharArray());

string output = string.Join(",", arrString);

Response.Write(output);

 

}


Output
Error,message,103,105,3125,5568,9806,9890,99233,Fail

 

Tags:

Linq Convert String Array To Int ArrayC# DateTime ToString()How To Declare and access array of string in C#Asp.net Convert String To Imagec# arraylistC# Convert String To DateTimeC# convert array to listC# foreach loopSplit String In C#C# Convert-List To ArrayC# Remove Trailing Comma Spaces And Zeros

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.