How to Get a Comma Separated String From an Array in C#?
Last Updated :
17 Jan, 2022
Given an array, now our task is to get a comma-separated string from the given array. So we can do this task using String.Join() method. This method concatenates the items of an array with the help of a comma separator between each item of the array.
Syntax:
String.Join(",", array_name)
Where array_name is the input array.
Example:
Input: {"sireesha", "priyank", "ojaswi", "gnanesh"}
Output: sireesha,priyank,ojaswi,gnanesh
Input: {"sireesha", "priyank"}
Output: sireesha,priyank
Approach 1:
- Declare an array of strings.
- Use the string join() function to get the comma separated strings.
String.Join(",", names)
- Display the final result.
Example:
C#
// C# program to display the comma separated
// string from an array
using System;
class GFG{
public static void Main()
{
// Creating an array of string elements
String[] names = { "sireesha", "priyank",
"ojaswi", "gnanesh" };
// Join the elements separated by comma
// Using Join() method
var str1 = String.Join(",", names);
// Display the final string
Console.WriteLine(str1);
}
}
Output:
sireesha,priyank,ojaswi,gnanesh
Approach 2:
We can also find the command-separated string from the object array.
- Create a class named MyEmployee with First_Name and Last_Name methods.
- Declare object array of MyEmployee in the main method.
- Use string join() function to get the comma separated strings.
String.Join(",", e.Select(m => m.First_Name));
Here, we only join the first name so we use the select method to select the First_Name of the employees.
- Display the final result.
Example 2:
C#
// C# program to display the comma separated
// string from an array
using System;
using System.Linq;
// MyEmployee class
class MyEmployee
{
public string First_Name { get; set; }
public string Last_Name { get; set; }
}
class GFG{
public static void Main()
{
// Creating object array of MyEmployee
MyEmployee[] e = {
new MyEmployee(){ First_Name = "Sumi", Last_Name = "Goyal" },
new MyEmployee(){ First_Name = "Mohan", Last_Name = "Priya" },
new MyEmployee(){ First_Name = "Sumit", Last_Name = "Singh" }
};
// Join the elements separated by comma
// Using Join() method
var res = String.Join(",", e.Select(m => m.First_Name));
// Display the final result
Console.WriteLine("Final String:" + res);
}
}
Output:
Final String:Sumi,Mohan,Sumit
Similar Reads
How to Remove Duplicate Values From an Array in C#? To remove duplicate values from an array in C#, you can use different approaches based on your requirements. So to do this we use the Distinct() function. HashSet, Using a Loop.This function gives distinct values from the given sequence. This method will throw ArgumentNullException if the given arra
3 min read
C# Program to Count Punctuation Characters in a String C# is a general-purpose, modern and object-oriented programming language pronounced as âC Sharpâ. In this article we will see the C# program, to count punctuation characters in a given string. Algorithm:First, create a string or get the string from the user.Declare a variable to count the number of
1 min read
C# Program to Split a String Collections into Groups Given a collection of strings and you are required to split them into groups using C#. The standard query operator contains GroupBy grouping operator using which we can split a collection of strings easily. The working of the GroupBy operator is similar to the SQL GroupBy clause. It is used to retur
3 min read
How to Convert a Comma-Separated String into an ArrayList in Java? In Java, to convert a comma-separated string into ArrayList, we can use the split() method to break the string into an array based on the comma delimiter. Another method we can also use Java Streams for a functional approach to achieve the same result.Example:Input: "Hello,from,geeks,for,geeks"Outpu
2 min read
How to Create Comma Separated List from an Array in PHP? The comma-separated list can be created by using implode() function. The implode() is a builtin function in PHP and is used to join the elements of an array.Syntax:string implode( separator, array )Return Type:The return type of implode() function is string. It will return the joined string formed f
2 min read
Convert ArrayList to Comma Separated String in Java ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. In order to convert ArrayList to a comma-separated String, these are the approaches available in Java as listed and proposed below as follows: Earlier before Java 8 there were
5 min read