I totally new to C# and learning as I go. I am stuck on issue which I'm hoping an experienced programmer can help. I have added a CheckedListBox to my form and added a collection of 6 items to it. I need all but the last item selected to have a comma placed beside it, so my question is: how can I tell C# NOT to place a comma beside the last item selected?
foreach (object itemChecked in RolesCheckedListBox.CheckedItems)
{
sw.Write(itemChecked.ToString() + ",");
}
Thanks for any help received! Dan
It can be done using string.Join() method:
string commaSeparated = string.Join(",",
RolesCheckedListBox.CheckedItems.Select(item => item.ToString());
For example:
string[] names = new []{ "a", "b"};
string separatedNames = string.Join(",", names);
Will result that separatedNames
will be "a,b"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With