Link to home
Create AccountLog in
Avatar of Mezillinu
MezillinuFlag for Malta

asked on

How do you extract text that is before a "-" in a short string example: 111 - Home. The 111 is an ID, I want to extract that

I have a string

Dim menu As String = ds.Item(0).ToString + " - " + ds.Item(1).ToString

And this example returns 111 - Home in a combobox, which when selected I want to return just its ID, which is 111.  How can I do this? Is there a simple method?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this quick tutorial will help you solve all these problems (and similar ones);
http://msconline.maconstate.edu/tutorials/VBNET/VBNET01/vbnet01-06.aspx

check out the instr and the left functions.
ASKER CERTIFIED SOLUTION
Avatar of chrissie1
chrissie1
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Use Item(0) as DataValue field and databind the datasource to combobox.
Avatar of Mezillinu

ASKER

the split method is an excellent idea.  it works! but how do I then extract after the first or second values for example?

when i quick watch it, i see that they were split correctly.
how do i extract from the array?

thanks
ok ok i got that :)
Dim values() As String = ComboBox1.Text.Split("-")
Debug.Print(values(0))
Debug.Print(values(1))
Debug.Print(values(2))
etc...

You can check to see how many items are in the array using .Length or .GetUpperBound:

    If values.Length >= 3 Then
        Debug.Print(values(0))
        Debug.Print(values(1))
        Debug.Print(values(2))
    End If

Or:

    If values.GetUpperBound(0) >= 2 Then
        Debug.Print(values(0))
        Debug.Print(values(1))
        Debug.Print(values(2))
    End If