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!
Printable View
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!
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
Hmm... I'm trying to figure out how to do that.. But can't :(
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
Awesome! It wokrs perfectly thanks. I forgot about the IsNumeric function.
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. :D
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
Shouldn't it be:)VB Code:
t = "There once [b]were[/b] 3 little pigs who had 3 little house[b]s[/b]."
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
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
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
Great answer da_silvy