The
TrimEnd
method and other methods to remove the
.
work. But the issue may be reassigning the modified list of items to the
ListBox
.
The
Items
property of
ListBox
is a
ReadOnly
property, hence it can be used to retrieve the existing list of items, but it cannot be assigned back. Hence, the
DataSource
property can be used to set the modified list.
The
TrimEnd('.')
works but if there are spaces at the end then it will not work. So, it is better to use
TrimEnd(new char[]{'.',' '})
to ensure that the
.
is removed even if there are trailing spaces. Since, it is required to remove only the . at the end of the string, I think it is a good option to use
TrimEnd
method.
The following code can be used to test the above points.
void Main()
{
Form form1 = new Form();
ListBox listBox1 = new ListBox();
listBox1.Items.AddRange(new string[]{"my name","her name","your name. "});
form1.Controls.Add(listBox1);
form1.ShowDialog();
var items = listBox1.Items.Cast<string>().Select (s => s.TrimEnd(new char[]{'.',' '})).ToList();
listBox1.DataSource=items;
form1.ShowDialog();
}