Q: Grabbing numbers from string
ahlers@twd.net (Chris Ahlers) wrote: In visual basic 4 i need help grabbing numbers from a string. I have several strings in a listbox that are followed by several spaces and then the numbers. ex. "Hello to all 45". I have tried using the val() method but that only seems to work if the...
A: try this... 'make a new project, a new form with a listbox and one commandbutton 'put the code in place (use Insert/File) 'press F5 Private Sub Command1_Click() Select Case Command1.Caption Case "&Grabbing only the numbers" list1.Clear list1.AddItem "testing one 1" list1.AddItem "testing two2" list1.AddItem "testing tree 3" list1.AddItem "testing four 4" list1.AddItem "5testing five" list1.AddItem "16 testing sixteen" list1.AddItem "700 testing sevenhunderd" list1.AddItem "testing 18 eightteen" Command1.Caption = "&Go" Case "&Go" Dim t% For t% = 0 To list1.ListCount - 1 list1.Selected(t%) = True MsgBox OnlyNumbers(list1) Next t% Command1.Caption = "&Grabbing only the numbers" End Select End Sub Private Function OnlyNumbers(source) As String Const Numbers = "1234567890" Dim t%, tmp$ OnlyNumbers = source tmp$ = "" For t% = 1 To Len(source) If InStr(Numbers, Mid$(source, t%, 1)) Then tmp = tmp & Mid$(source, t%, 1) End If Next t% OnlyNumbers = tmp$ End Function Return