|
-
Aug 10th, 2000, 07:37 AM
#1
Thread Starter
New Member
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.
-
Aug 10th, 2000, 07:47 AM
#2
Addicted Member
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
-
Aug 10th, 2000, 08:02 AM
#3
Frenzied Member
Try
Code:
mynumber = val(mystring)
Harry.
"From one thing, know ten thousand things."
-
Aug 10th, 2000, 08:13 AM
#4
Member
Not bad !
Your answer isn't so bad Steven, i was going to post the same answer
-
Aug 10th, 2000, 08:15 AM
#5
Fanatic Member
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!
-
Aug 10th, 2000, 08:19 AM
#6
Frenzied Member
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."
-
Aug 10th, 2000, 08:21 AM
#7
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|