Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create a comma separated string from a list of string in C#?
A List of strings can be converted to a comma separated string using the built-in string.Join() method. This method takes a separator and a collection of strings, returning a single concatenated string.
This type of conversion is useful when collecting user data (such as checkbox selections) and converting it to a comma separated string for database queries or API calls.
Syntax
Following is the syntax for string.Join() method −
string.Join(separator, collection);
Parameters
-
separator − The string to use as separator between elements
-
collection − A collection that contains the strings to concatenate
Return Value
Returns a string that consists of the elements in the collection delimited by the separator string. If the collection is empty, the method returns an empty string.
Using string.Join() with List<string>
Example
using System;
using System.Collections.Generic;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
List<string> fruitsList = new List<string> {
"banana",
"apple",
"mango"
};
string fruits = string.Join(",", fruitsList);
Console.WriteLine(fruits);
}
}
}
The output of the above code is −
banana,apple,mango
Using string.Join() with Complex Objects
You can extract properties from a list of complex objects using LINQ's Select() method and then join them −
Example
using System;
using System.Collections.Generic;
using System.Linq;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
var studentsList = new List<Student> {
new Student {
Id = 1,
Name = "John"
},
new Student {
Id = 2,
Name = "Jack"
}
};
string students = string.Join(",", studentsList.Select(student => student.Name));
Console.WriteLine(students);
}
}
public class Student {
public int Id { get; set; }
public string Name { get; set; }
}
}
The output of the above code is −
John,Jack
Using Different Separators
Example
using System;
using System.Collections.Generic;
namespace DemoApplication {
public class Program {
static void Main(string[] args) {
List<string> colors = new List<string> { "Red", "Green", "Blue" };
string commaDelimited = string.Join(",", colors);
string pipeDelimited = string.Join(" | ", colors);
string spaceDelimited = string.Join(" ", colors);
Console.WriteLine("Comma: " + commaDelimited);
Console.WriteLine("Pipe: " + pipeDelimited);
Console.WriteLine("Space: " + spaceDelimited);
}
}
}
The output of the above code is −
Comma: Red,Green,Blue Pipe: Red | Green | Blue Space: Red Green Blue
Conclusion
The string.Join() method provides an efficient way to convert a list of strings into a single delimited string. It works with any IEnumerable<string> collection and allows you to specify any separator string, making it ideal for creating CSV data, SQL query parameters, or formatted output.
