简体   繁体   中英

C# How to place a comma after each word but the last in the list

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"

Here are some articles I wrote about this problem:

http://blogs.msdn.com/b/ericlippert/archive/2009/04/13/restating-the-problem.aspx

http://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx

The comments to the second article have dozens of solutions to a slightly harder version of the problem; it is educational to browse those solutions and see which ones you find elegant and which ones you find gross.

A way to enhance your example could be:

var index = 0;
foreach (object itemChecked in RolesCheckedListBox.CheckedItems)
{
    if ( index>0 ) sw.Write( "," );
    sw.Write(itemChecked.ToString());
    index++;
}

Not elegant, but working.

您可以使用Join函数而不是循环:

sw.Write(string.Join(",", RolesCheckedListBox.CheckedItems));

在4.0中,您可以简单地使用string.Join(字符串分隔符,IEnumerable值)为所有非空值调用ToString()方法:

var commaSeparated = string.Join(",", RolesCheckedListBox.CheckedItems);
  1. transform your ListItemCollection to a string array using linq
  2. use String.Join

Like:

public string ItemsToString(ListItemCollection items)
{
    string[] stringArray = (from ListItem item in items where item.Selected select item.ToString()).ToArray();
    return String.Join(", ", stringArray);
}

you could use it by

sw.Write(ItemsToString(RolesCheckedListBox.CheckedItems));

Or if you prefer a single line for the job:

sw.Write(String.Join(", ", (from ListItem item in RolesCheckedListBox.CheckedItems select item.ToString()).ToArray()));

In .Net 4 you can use static String.Join<T> Method (String, IEnumerable<T>) and print concatenated string.

Otherwise the easy way is to prepend comma to all elements but the first one:

bool shouldPrependComma = false;
foreach (object itemChecked in RolesCheckedListBox.CheckedItems) 
{
  if (shouldPrependComma) sw.Write(",");
  sw.Write(itemChecked.ToString());
  shouldPrependComma = true;
}
foreach (object itemChecked in RolesCheckedListBox.Items)
            {
                if (itemChecked != RolesCheckedListBox.Items[RolesCheckedListBox.Items.Count - 1])
                    sw.Write(itemChecked.ToString() + ",");
            }

That should help you. Also, I just used "Items", you used CheckedItems. Change all instances of Items to CheckedItems if you want to only iterate through checked items.

Jon Skeet为这类问题实施了SmartEnumerable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM