Search code examples
c#linqsplitstring-concatenation

Add comma between values by using Linq


My values come from ComboBox:

2|722|742|762|77

I delete unnecessary characters as follows:

foreach (var item in checkListBox)
{
    string[] list = item.Split(
        new string[] { "2|" },
        StringSplitOptions.RemoveEmptyEntries);
}

My list values result:

"72"
"74"
"76"
"77"

My question is:

how can I get all of the above values in 1 row (next to each other) separated by comma like this:

72,74,76,77

?


Solution

  • How about

    var result = string.Join(",", item.Split(new string[] { "2|" }, StringSplitOptions.RemoveEmptyEntries));