We can get a comma-separated string from an array using String.Join() method.
string[] animals = { "Cat", "Alligator", "Fox", "Donkey" };
var str = String.Join(",", animals);
In the same way, we can get a comma-separated string from the integer array.
int[] nums = { 1, 2, 3, 4 };
var str = String.Join(",", nums);
We can also get a comma separated string from the object array, as shown below.
Person[] people = {
new Person(){ FirstName="Steve", LastName="Jobs"},
new Person(){ FirstName="Bill", LastName="Gates"},
new Person(){ FirstName="Lary", LastName="Page"}
};
var str = String.Join(",", people.Select(p => p.FirstName) );
Thus, we can easily get the string with comma separated or any other separator from the array in C#.
Related Articles
- How to validate email in C#?
- How to get the sizeof a datatype in C#?
- Difference between String and StringBuilder in C#
- Static vs Singleton in C#
- Difference between == and Equals() Method in C#
- Asynchronous programming with async, await, Task in C#
- How to loop through an enum in C#?
- Generate Random Numbers in C#
- Difference between Two Dates in C#
- Convert int to enum in C#
- BigInteger Data Type in C#
- Convert String to Enum in C#
- Convert an Object to JSON in C#
- Convert JSON String to Object in C#
- DateTime Formats in C#
- How to convert date object to string in C#?
- Searching in C# array
- Compare strings in C#
- How to count elements in C# array?
- How to combine two arrays without duplicate values in C#?
- Difference between String and string in C#.
- How to remove duplicate values from an array in C#?
- How to sort an array in C#?
- How to sort object array by specific property in C#?
- Boxing and Unboxing in C#
- How to convert string to int in C#?
- Difference between Array and ArrayList