This is a very common requirement for C# developers: how to convert an array to a comma separated string in C#.
Below are the different methods to convert an array to a comma-separated string in C#.
- Using a For Loop
- Using String.Join()
- Using StringBuilder
- Using LINQ
Convert array to comma separated string c#
We will see all 4 methods with complete examples with code you can run using a Visual Studio console application.
1. Using a For Loop
One of the simplest ways to convert an array to a comma-separated string is by using a for
loop in C#. Here is the complete code:
using System;
namespace ArrayToCommaSeparatedString
{
class Program
{
static void Main(string[] args)
{
// Initialize an array with U.S. city names
string[] cities = { "New York", "Chicago", "San Francisco" };
// Declare a variable to hold the resulting comma-separated string
string result = "";
// Loop through the array to concatenate the city names
for (int i = 0; i < cities.Length; i++)
{
result += cities[i];
// Add a comma and space, except after the last element
if (i < cities.Length - 1)
{
result += ", ";
}
}
// Print the resulting comma-separated string
Console.WriteLine(result);
}
}
}
Once you run the code using Visual Studio, you can see the output in the screenshot below:
The pros of this method is that, it is a simple and straightforward approach, but it is not the most efficient for large arrays.
2. Using String.Join()
The C# String.Join()
method offers a more modern and efficient approach for converting an array to a comma-separated string. So this is another useful method you can use to convert an array to comma separated string c#.
using System;
namespace UsingStringJoin
{
class Program
{
static void Main(string[] args)
{
// Initialize an array with U.S. city names
string[] cities = { "New York", "Chicago", "San Francisco" };
// Use String.Join() to create a comma-separated string
string result = String.Join(", ", cities);
// Print the resulting comma-separated string
Console.WriteLine(result);
}
}
}
3. Using StringBuilder
If you are dealing with a large array, then you can use StringBuilder to convert an array to comma separated string c#. This is slightly more complex than other methods but efficient for large arrays. Here is the complete code.
using System;
using System.Text;
namespace UsingStringBuilder
{
class Program
{
static void Main(string[] args)
{
// Initialize an array with U.S. city names
string[] cities = { "New York", "Chicago", "San Francisco" };
// Use StringBuilder for concatenation
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cities.Length; i++)
{
sb.Append(cities[i]);
// Add a comma and space, except after the last element
if (i < cities.Length - 1)
{
sb.Append(", ");
}
}
// Convert StringBuilder to string
string result = sb.ToString();
// Print the resulting comma-separated string
Console.WriteLine(result);
}
}
}
4. Using LINQ
We can also use LINQ to convert an array to a comma-separated string in C#. Here is the complete code.
using System;
using System.Linq;
namespace UsingLINQ
{
class Program
{
static void Main(string[] args)
{
// Initialize an array with U.S. city names
string[] cities = { "New York", "Chicago", "San Francisco" };
// Use LINQ to create a comma-separated string
string result = string.Join(", ", cities.Select(x => x.ToString()));
// Print the resulting comma-separated string
Console.WriteLine(result);
}
}
}
Once you run the code, you can see the output in the screenshot below:
Conclusion
This complete tutorial explored 4 methods to convert an array to a comma-separated string in C#.
- Using a For Loop
- Using String.Join()
- Using StringBuilder
- Using LINQ
You may also like:
- How to Convert Char Array to String in C#.NET?
- How to reverse a string in C# using Linq?
- C# String Array Contains Exact Match Example
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…