Results 1 to 7 of 7

Thread: How to find a number in a string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 1999
    Posts
    15

    Unhappy

    How to find a number in a string?
    When i get a string :
    mystring = "jkj kj j jjjk 4.235kkik"
    i want
    mynumber = 4.235

    The number doesn't always stand on the same place
    in the string.


    Thanks.

  2. #2
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    Try this hack, I'm sure someone will post a better response.

    Code:
    Private Sub Command28_Click()
        Dim mystring As String
        Dim mynumber As Long
        Dim x As Integer
        Dim mytemp As Variant
        
        mystring = "jkj kj j jjjk 4.235kkik"
        
        For x = 1 To Len(mystring)
            If (IsNumeric(Mid(mystring, x, 1)) Or Mid(mystring, x, 1) = ".") Then
                mytemp = mytemp & Mid(mystring, x, 1)
            End If
        Next x
            
        'May need some conversion to Long here ???
        mynumber = mytemp
    
    
    End Sub

  3. #3
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Try

    Code:
    mynumber = val(mystring)
    Harry.

    "From one thing, know ten thousand things."

  4. #4
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Wink Not bad !

    Your answer isn't so bad Steven, i was going to post the same answer
    Hi-Tech Engineer

  5. #5
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    Afraid not harry. The val function does work to en extent, but if the string does not start with a Number then you will get a value of 0.

    The way it works is that it looks through the string and sees if it is a number. Upon hitting the first non-numerical character it returns what it has found.

    Iain, thats with an i by the way!

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    On second thoughts try this

    Code:
    Dim thestr As String, thenum As Double
    thestr = "abcd de 39.76f alw"
    For x = 1 To Len(thestr)
        If IsNumeric(Mid(thestr, x, 1)) Then thenum = Val(Mid(thestr, x)): Exit For
    Next
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Darn you beat me to it Iain I remembered that just after I posted *Doh*
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width