Results 1 to 12 of 12

Thread: Getting only numbers out of a string

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Getting only numbers out of a string

    Alright, say I have a string called "sPhrase" and sPhrase was set to "There once was 3 little pigs who had 3 little house." How can I eliminated everything but the numbers, so instead of that I want "33"

    Thanks in advance!
    ~!~Computer Nerd~!~

  2. #2
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting only numbers out of a string

    loop each char and check to see if the character is a number and place that char in a buffer for all numbers you've encountered in the phrase

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Getting only numbers out of a string

    Hmm... I'm trying to figure out how to do that.. But can't
    ~!~Computer Nerd~!~

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting only numbers out of a string

    not tested code

    VB Code:
    1. Function getNumStr(ByVal strPhrase As String) As String
    2.     getNumStr = ""
    3.     For i = 1 to Len(strPhrase)
    4.         If IsNumeric(Mid(strPhrase, i , 1)) Then
    5.             getNumStr = getNumStr & Mid(strPhrase, i, 1)
    6.         End If
    7.     Next i
    8. End Function
    Last edited by oceanebelle; Aug 18th, 2005 at 10:33 PM.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2004
    Location
    Computer
    Posts
    56

    Re: Getting only numbers out of a string

    Awesome! It wokrs perfectly thanks. I forgot about the IsNumeric function.
    ~!~Computer Nerd~!~

  6. #6
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Getting only numbers out of a string

    but then i just found out, Is numberic evaluates HEX numbers as well, thus it may treat.. a, b, c, e as number... do check on that ... and if it is.. the just check the character by using select case.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Getting only numbers out of a string

    This works:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.   Dim t$, y$
    5.   Dim x As Integer, str$
    6.   t = "There once was 3 little pigs who had 3 little house."
    7.   For x = 1 To Len(t)
    8.     y = Mid$(t, x, 1)
    9.     Select Case y
    10.       Case "0" To "9"
    11.         str = str & y
    12.     End Select
    13.   Next x
    14.   MsgBox str
    15. End Sub

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Getting only numbers out of a string

    Shouldn't it be
    VB Code:
    1. t = "There once [b]were[/b] 3 little pigs who had 3 little house[b]s[/b]."

  9. #9
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Getting only numbers out of a string

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.  
    5.     Dim Str As String
    6.    
    7.     Str = "Now is the time for all 45 good men to come to the aid of the 4th party"
    8.     Debug.Print ExtractNumber(Str)
    9.    
    10. End Sub
    11.  
    12. Public Function ExtractNumber(Phrase As String) As String
    13.  
    14.     Dim Phr() As Byte
    15.     Dim i As Long
    16.     Dim n As Long
    17.    
    18.     Phr = Phrase
    19.     n = UBound(Phr)
    20.    
    21.     For i = 0 To n Step 2
    22.         If Phr(i) > 47 And Phr(i) < 58 Then
    23.             ExtractNumber = ExtractNumber & ChrW$(Phr(i))
    24.         End If
    25.     Next
    26.    
    27. End Function
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  10. #10
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Getting only numbers out of a string

    Hey, I guess all mostly wrong. The codes given are only for integer. What about if the numbers in the strings are such:

    "There are 4.5768 million people in the world having AIDS"

    Can I get the 4.5768?

    Pls help. Tq

  11. #11
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: Getting only numbers out of a string

    Option Explicit

    Private Sub Form_Load()

    Dim Str As String

    Str = "Now is the time for all 45 good men to come to the aid of the 4th party"
    Debug.Print ExtractNumber(Str)

    End Sub

    Public Function ExtractNumber(Phrase As String) As String

    Dim Phr() As Byte
    Dim i As Long
    Dim n As Long

    Phr = Phrase
    n = UBound(Phr)

    For i = 0 To n Step 2
    If Phr(i) > 47 And Phr(i) < 58 Or Phr(i) = Asc(".") Then
    ExtractNumber = ExtractNumber & ChrW$(Phr(i))
    End If
    Next

    End Function

  12. #12
    Lively Member
    Join Date
    Jan 2006
    Posts
    80

    Re: Getting only numbers out of a string

    Great answer da_silvy

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