User1899289003 User1899289003 - 16 days ago 5
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
Value
deleting
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

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();
}
Comments