How to join List<string> into one string with a separator in C#?
Hello, colleagues! I'm new to C# and I've encountered this problem: I have a list of strings `List`, and I need to join all elements into one string, inserting some separator between them, like a comma or semicolon.
Here's an example code:
```csharp
List fruits = new List { "Apple", "Banana", "Orange", "Pear" };
// I want to get the string: "Apple, Banana, Orange, Pear"
```
I tried iterating through the list with a `foreach` loop and adding strings to `StringBuilder`, but it looks cumbersome and I'm sure there's a more elegant solution.
I tried something like `string result = string.Join(fruits, ", ");` but the compiler complains about the argument order. Also, I've heard about LINQ and `Aggregate`, but I'm afraid of getting confused with the syntax.
Please advise how to
No answers yet.
Log in to leave an answer