String Manipulation Question

  • Thread starter Thread starter Jack
  • Start date Start date
J

Jack

Hi,

I am trying to extract a piece out of a string like the below:

1234-01 and I want to retrieve the piece before the - so 1234
1234 no - so I just want it as is
123456-01 so 123456

I have been trying to use split function but I cannot seem to get to work.
Any ideas?

Thanks
 
Nevermind I figured it out. I used split function with the following code:
string test = "1234-01";
string [] test2 = test.Split('-');

test2[0].ToString() = "1234"
test2[1].ToString() = "01"
 
Try this:

string sz = "12345-123"; // or "12345"
string result = sz.Substring(0, sz.IndexOf("-")==-1 ? sz.Length : sz.IndexOf("-") );
MessageBox.Show(result);
 
Back
Top