User1899289003 User1899289003 - 1 year ago 69
C# Question

remove characters and blank space from string

I have a listbox with for example this values:

1 - Value

55 - Value

What can I do to convert my string and select only
55 - "blank space"
I think that maybe I could do 2 splits, one to delete the
and the other to delete the number, but I want to know a simple way to do this.

Answer Source

You could just use substring:

var idx = listBox.SelectedItem.ToString().IndexOf('-');
string val = listBox.SelectedItem.ToString().SubString(idx+1).Trim();

or more defensive:

var idx = value1.IndexOf('-');
if( idx != -1 ){
   string val = value1.SubString(idx+1).Trim();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download