Labels

slider

Recent

Navigation

How to get a comma separated string from a list in C#?

Learn how to get a comma separated (CSV) string from a list in C# and get examples with codes. Find methods to convert list into CSV record string!
How to get a comma separated string from a list in C#?

Introduction

Hope you must have gone through my previous articles on C#, Interview on OOPS Interview question and Answers. Now we’ll discuss on List to CSV string conversion method. Let's now discuss on List to CSV string conversion method. You’re required to use the string.Join method and StringBuilder to convert lists to strings and vice versa.

Covert List, String

At the time of conversion of the list and string, just imagine a sentence, which contains some words. We could display a sentence as a single string, i.e. the sentence with spaces. But, a list of strings or words is something different, i.e. chain of words uniquely separated by comma or lines/hyphens.

Hence, by using C# codes, we can convert our list into a string and vice versa. 

Convert List into CSV record

To gather a list of strings (words) into one string, we generally use the string.Join method. The output can be displayed as a CSV record. In an earlier version of the ‘.Net framework’, the ‘ToArray’ method was used but, the ‘ToArray’ method is not used in the new .NET Framework versions.

Join List of strings into one string through ‘string.Join’ method 

However, here we need to use the ‘String.join’ method instead of calling the ‘ToArray’ method on a List. Following is a C# program that converts List through the ‘string.Join’ method.

Output

.Net Core, Angular, SQL, React, Jquery

Use of ‘StringBuilder’ class to convert List to a string

Here the ‘StringBuilder’ class is used to convert a List to a single string. The specific benefit of using this class is that you can convert a List of any types of objects into a string this method.

Following examples of C# program has contained List and StringBuilder

Output

.Net Core| Angular| SQL| React| Jquery|

Notes on the above examples

Final delimiter: The above program has a final delimiter on the end which is not present in ‘string.Join’ method. Hence, inconvenience is avoided here.

TrimEnd: Sometimes, it is recommended to replace the end delimiter using TrimEnd or you can keep left alone.

TrimStart: It’s also used to remove some unnecessary things at the beginning of the sentence.

Convert List of ints into a string through StringBuilder class

Similarly, a ‘List of ints’ can be converted into a single string using the Append method of StringBuilder as this append method accepts the different types of variables. So, you can simply pass the int into it.

Append() method converts the ‘int’ to a string and appends the same properly.

StringBuilder is a faster program for converting ‘int’ to a string through a Char Array (char[]).

Following is a simple C# program that converts List types of ‘int’ into a string

Output

7,9,18,27,25

We can also get a CSV string from the array of objects as follows.

Output

.Net Core, Angular, ReactJS

Thus, we can easily get the string with (comma or any other separators) from the array of the object in the C# language.

Convert CSV strings to List of strings through the SPLIT method

When we need to make a list of strings from a CSV string format, we need to use the Split method. 

If you need an items list, just loop over the array of string returned by the Split method.

Following is a simple C# program to convert string to List:

Output

Technology

Crowds

Tutorial

Get a CSV string from an array using String.Join() method

We can get a CSV string from an array of strings through String.Join() method.

string[] animals = { "Technology", "Harsh", "Kish" };

var str = String.Join(",", animals);

Output

Technology,Harsh,Kish

Similarly, we can have a CSV string from the array of integers.

int[] nums = { 9,18,25,27 };
var str = String.Join(",", nums); 

Output

9,18,25,27

Wrapping Up

Hope, you must have learned well about the use of ‘string.Join’ and StringBuilder methods to convert list of items to strings (Comma separated value).  Similarly, the Split method is used to get list of items from string (CSV format). Also, getting CSV string from an array of string is a little bit similar to getting a CSV string from a list of items.
Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: