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
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5