|
-
Aug 18th, 2005, 10:17 PM
#1
Thread Starter
Member
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!
-
Aug 18th, 2005, 10:20 PM
#2
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
-
Aug 18th, 2005, 10:23 PM
#3
Thread Starter
Member
Re: Getting only numbers out of a string
Hmm... I'm trying to figure out how to do that.. But can't
-
Aug 18th, 2005, 10:29 PM
#4
Re: Getting only numbers out of a string
not tested code
VB Code:
Function getNumStr(ByVal strPhrase As String) As String
getNumStr = ""
For i = 1 to Len(strPhrase)
If IsNumeric(Mid(strPhrase, i , 1)) Then
getNumStr = getNumStr & Mid(strPhrase, i, 1)
End If
Next i
End Function
Last edited by oceanebelle; Aug 18th, 2005 at 10:33 PM.
-
Aug 18th, 2005, 10:35 PM
#5
Thread Starter
Member
Re: Getting only numbers out of a string
Awesome! It wokrs perfectly thanks. I forgot about the IsNumeric function.
-
Aug 18th, 2005, 10:45 PM
#6
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.
-
Aug 18th, 2005, 11:01 PM
#7
Re: Getting only numbers out of a string
This works:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim t$, y$
Dim x As Integer, str$
t = "There once was 3 little pigs who had 3 little house."
For x = 1 To Len(t)
y = Mid$(t, x, 1)
Select Case y
Case "0" To "9"
str = str & y
End Select
Next x
MsgBox str
End Sub
-
Aug 19th, 2005, 01:05 AM
#8
Re: Getting only numbers out of a string
Shouldn't it be
VB Code:
t = "There once [b]were[/b] 3 little pigs who had 3 little house[b]s[/b]."
-
Aug 19th, 2005, 06:15 AM
#9
Frenzied Member
Re: Getting only numbers out of a string
VB Code:
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 Then
ExtractNumber = ExtractNumber & ChrW$(Phr(i))
End If
Next
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
-
Apr 4th, 2006, 12:45 AM
#10
Lively Member
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
-
Apr 4th, 2006, 02:22 AM
#11
Conquistador
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
-
Apr 4th, 2006, 03:45 AM
#12
Lively Member
Re: Getting only numbers out of a string
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|