Convert a List to a String using delimiter in C#

Google Translate Icon

This post will discuss how to convert a list to a string in C# where individual elements of the list are joined into a single string using a given delimiter. The delimiter should not be added before or after the list.

1. Using String.Join() method

The recommended solution is to use the String.Join() method of the string class, which joins elements of the specified array or collection together with the specified delimiter.

Download  Run Code

2. Using Enumerable.Aggregate() method

Like the above method, LINQ provides the Aggregate() method, which can join elements of a list using a delimiter. The following code example shows how to implement this:

Download  Run Code

3. Using StringBuilder

A naive solution is to loop through the list and concatenate each list element to the StringBuilder instance with the specified delimiter. Finally, we return the string representation of the StringBuilder. Note that the solution handles trailing delimiters’ characters.

Download  Run Code

That’s all about converting a List to a String using a delimiter in C#.

Rate this post

Average rating 4.83/5. Vote count: 23

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!