|
-
May 18th, 2001, 08:10 AM
#1
Thread Starter
Hyperactive Member
Find a ! in a string
Please tell me there is a shorter/cleaner way? Basically if ! is in a text box or string.... let the msgbox come up.
For FindCommand = 1 To Len(StringName)
If Mid$(StringName, FindCommand, Len("!")) = "!" Then
msgbox "It's In there!"
Exit For
End If
Next FindCommand
-RaY
VB .Net 2010 (Ultimate)
-
May 18th, 2001, 08:13 AM
#2
If InStr(StringName, "!") > 0 Then
MsgBox "It's In there!"
End If
-
May 18th, 2001, 08:14 AM
#3
Lively Member
How about this?
Dim SearchString, SearchChar, MyPos
SearchString ="XX!XX" ' String to search in.
SearchChar = "!" ' Search for "!".
' A textual comparison starting at position 1. Returns 3.
MyPos = Instr(1, SearchString, SearchChar, 1)
Real programs don't eat cache.
Pete 
-
May 18th, 2001, 08:22 AM
#4
Thread Starter
Hyperactive Member
Well the whole point was to make it shorter... and Tugur your's worked fine! Exactly what I was looking for, thanks!
-RaY
VB .Net 2010 (Ultimate)
-
May 18th, 2001, 08:46 AM
#5
Addicted Member
but theyre both the same ?????????????
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
May 18th, 2001, 10:59 AM
#6
You could also use the Like operator.
Code:
If StringName Like "*!*" Then Msgbox "StringName contains '!'"
-
May 18th, 2001, 01:38 PM
#7
If it matters, InStr is much faster than Like.
-
May 18th, 2001, 02:08 PM
#8
GetTickCount (API): InStr = 0 Like = 25 [milliseconds]
Timer (function): InStr = 0 Like = 0 [seconds]
QueryPerformanceCounter (API):
InStr - 0.4063
Like - 0.3523
Difference is 0.054 miliseconds
But in some cases, InStr is faster.
So I'm unsure, just givin' you some results from testin' it though .
-
May 18th, 2001, 02:19 PM
#9
Here's my test:
Start a new project and put a command button on the form. Then put this code in the Click event of the command button:
Code:
Dim ISt As Single, IE As Single
Dim LSt As Single, LE As Single
Dim B As Boolean
Dim I As Long
Const Times = 1000000
Dim TestStr As String
TestStr = "hi!"
Me.AutoRedraw = True
Me.FontName = "Fixedsys"
ISt = Timer
For I = 1 To Times
B = InStr(TestStr, "!") > 0
Next I
IE = Timer
LSt = Timer
For I = 1 To Times
B = TestStr Like "*!*"
Next I
LE = Timer
Cls
Print "InStr: "; vbTab; IE - ISt
Print "Like: "; vbTab; LE - LSt
As far as I know, there are no errors in that test code. It shows InStr to be consistently faster than Like.
-
May 18th, 2001, 02:29 PM
#10
While Like may be slightly slower, it's the prefered method for pattern matching, whereas InStr is prefered for searching.
-
May 18th, 2001, 02:35 PM
#11
I would say that in this case we're searching for an exclamation point and showing a messagebox if it is found.
You can make Like search for anything by putting *'s around it, but InStr is better suited for the task.
-
May 18th, 2001, 09:20 PM
#12
Actually Tygur, I believe it's best suited for the job.
fallnwrld says himself, "Please tell me there is a shorter/cleaner way? Basically if ! is in a text box or string.... let the msgbox come up."
So he just wants to see if an exclamation exists in the Textbox, not find the location of where it is in the Textbox.
-
May 18th, 2001, 09:47 PM
#13
Just a couple statements:
I hardly believe searching for an exclamation point is pattern matching.
InStr does the job quicker. Therefore it must be more set for the task than Like.
Just because InStr gives more information does not make it worse. I don't know where you get that from..
If the speed doesn't matter, it just comes down to personal preference. I think InStr is more readable and I know it's faster, so I prefer InStr.
-
Jul 10th, 2001, 10:44 AM
#14
Originally posted by Tygur
Just a couple statements:
I think InStr is more readable and I know it's faster, so I prefer InStr.
Just real quickly see which one is faster for searching on a textbox.
Code:
Private Sub Form_Load()
Dim ISt As Single, IE As Single
Dim LSt As Single, LE As Single
Dim B As Boolean
Dim I As Long
Const Times = 1000000
Dim TestStr As String
Text1.Text = "hi!"
Me.AutoRedraw = True
Me.FontName = "Fixedsys"
ISt = Timer
For I = 1 To Times
B = InStr(Text1.Text, "!") > 0
Next I
IE = Timer
LSt = Timer
For I = 1 To Times
B = Text1.Text Like "*!*"
Next I
LE = Timer
Cls
Print "InStr: "; vbTab; IE - ISt
Print "Like: "; vbTab; LE - LSt
End Sub
-
Jul 10th, 2001, 10:53 AM
#15
Originally posted by Tygur
and I know it's faster, so I prefer InStr.
Like is almost twice as fast as InStr
-
Jul 10th, 2001, 10:55 AM
#16
Originally posted by Megatron
Like is almost twice as fast as InStr
I get the same result
-
Jul 10th, 2001, 11:22 AM
#17
I just copied and ran the code posted by rickm. It showed InStr to be faster for me. I have no idea why it would show Like to be faster (let alone almost twice as fast) on another computer.
-
Jul 10th, 2001, 11:38 AM
#18
Originally posted by Tygur
I just copied and ran the code posted by rickm. It showed InStr to be faster for me. I have no idea why it would show Like to be faster (let alone almost twice as fast) on another computer.
I dont either but when i run my code i get consistently that like is faster, when i run your code i dont consistently get any particular result, but Matthew Gates' testing suggests that Like is faster as well, and Megatron is a freakin genius so he probably actually just knows which one is faster.
-
Jul 10th, 2001, 11:40 AM
#19
Member
As a completely impartial view I also ran the above code and got the following result:
InStr: 10.62891
Like: 11.375
This is on an intel celeron 533mhz
IUnknown
Using VB6.0 (sp5), SQL Server 2000, Visual Studio .Net
-
Jul 10th, 2001, 11:44 AM
#20
Originally posted by IUnknown
As a completely impartial view I also ran the above code and got the following result:
InStr: 10.62891
Like: 11.375
This is on an intel celeron 533mhz
IUnknown
Im running on 1.3 GHz, dont know if it makes a difference, i have 800MHz at home, ill try it there.
-
Jul 10th, 2001, 01:06 PM
#21
rickm,
1.3 Ghz what? What's the processor? I doubt the clock speed means anything, but the processor might.
These are my results on a 900 Mhz Athlon:
InStr: 4.617188
Like: 4.707031
InStr is consistently faster every time I run it.
Also, if Megatron does "just know", it's probably because he found out the same way we're trying right now.
-
Jul 10th, 2001, 01:55 PM
#22
I tried it on a 1.3GHz P4 at work, and a 1.2GHz Athlon at home. Both say like is faster, and both are faster on the athlon machine.
-
Jul 10th, 2001, 06:20 PM
#23
Registered User
In this particular situation which of course is a matter of life and death, I am with Tygur, the like operator is twice as slow as instr(), here is the test:
VB Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim start As Double, test As String
test = test & String(25000000, "7")
test = test & "!"
start = GetTickCount
Debug.Print "instr: " & InStr(1, test, "!")
Debug.Print "instr: " & GetTickCount - start
start = GetTickCount
Debug.Print "like: " & test Like "*!*"
Debug.Print "like: " & GetTickCount - start
End Sub
Run on a PIII
-
Jul 11th, 2001, 12:41 AM
#24
Originally posted by Nucleus
In this particular situation which of course is a matter of life and death, I am with Tygur, the like operator is twice as slow as instr(), here is the test:
VB Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim start As Double, test As String
test = test & String(25000000, "7")
test = test & "!"
start = GetTickCount
Debug.Print "instr: " & InStr(1, test, "!")
Debug.Print "instr: " & GetTickCount - start
start = GetTickCount
Debug.Print "like: " & test Like "*!*"
Debug.Print "like: " & GetTickCount - start
End Sub
Run on a PIII
again, try the textbox
Code:
Private Sub Command1_Click()
Dim start As Double, test As String
test = test & String(25000000, "7")
test = test & "!"
Text1.Text = test
start = GetTickCount
Debug.Print "instr: " & InStr(1, Text1.Text, "!")
Debug.Print "instr: " & GetTickCount - start
start = GetTickCount
Debug.Print "like: " & Text1.Text Like "*!*"
Debug.Print "like: " & GetTickCount - start
End Sub
run on a Duron 800
-
Jul 11th, 2001, 01:07 AM
#25
Registered User
rickm, the text box should have no impact on the relative speed of Instr() vs like.
Out of interest when you ran the code I posted what were your results?
-
Jul 11th, 2001, 01:15 AM
#26
Hyperactive Member
Right On Nuc,
Textbox.text extracts the contents to temporary string.
That OO. Nothing "operates" on the contents of an object
except through the object's methods. In this case, .text is
a Get property for the contents. Instr and the like never see
the object whence the string came.
Also, for those who get faster times on Like I hope they were not
testing with an Instr(str,"!",vbTextCompare). I'm not sure of the effect of vbTextCompare on Instr even though the search character is not an alpha. Perhaps we could get timings on that. I'm going to loop it right now.
-
Jul 11th, 2001, 01:43 AM
#27
Hyperactive Member
Tested in the IDE. 700mz HP Celeron
Code:
Dim I As Long
Dim J As Long
strResponse = Space(1000) & "!"
Debug.Print Now & " NoTextcompare"
For I = 1 To 500000
J = InStr(1, strResponse, "!")
Next
Debug.Print Now & " Textcompare"
For I = 1 To 500000
J = InStr(1, strResponse, "!", vbTextCompare)
Next
Debug.Print Now & " Like"
For I = 1 To 500000
blnOK = (strResponse Like "*!*")
Next
Debug.Print Now
7/11/2001 2:26:45 AM NoTextcompare
7/11/2001 2:26:48 AM Textcompare
7/11/2001 2:28:07 AM Like
7/11/2001 2:28:11 AM
Dropped vbTextCompare, bump times to 10,000,000 and
put "!" the middle where Instr would stop but Like wouldn't.
Code:
Dim I As Long
Dim J As Long
strResponse = Space(500) & "!" & Space(500)
Debug.Print Now & " NoTextcompare"
For I = 1 To 10000000
J = InStr(1, strResponse, "!")
Next
Debug.Print Now & " Like"
For I = 1 To 10000000
blnOK = (strResponse Like "*!*")
Next
Debug.Print Now
got
7/11/2001 2:32:41 AM NoTextcompare
7/11/2001 2:33:20 AM Like
7/11/2001 2:34:01 AM
I call the whole thing a tie on my machine.
-
Jul 11th, 2001, 01:58 AM
#28
Registered User
John I get different results for different compare methods which you correctly raised a factor which impacts on speed.
I found binary compare being the fastest, but instr() consistently outperforms like operator under all compare methods .
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
Dim start As Double, test As String
test = test & String(25000000, "7")
test = test & "!"
start = GetTickCount
Debug.Print "instr no compare method: " & InStr(1, test, "!")
Debug.Print "instr no compare method: " & GetTickCount - start
start = GetTickCount
Debug.Print "instr binary compare: " & InStr(1, test, "!", vbBinaryCompare)
Debug.Print "instr binary compare: " & GetTickCount - start
start = GetTickCount
Debug.Print "instr text compare: " & InStr(1, test, "!", vbTextCompare)
Debug.Print "instr text compare: " & GetTickCount - start
start = GetTickCount
Debug.Print "like: " & test Like "*!*"
Debug.Print "like: " & GetTickCount - start
End Sub
-
Jul 11th, 2001, 02:07 AM
#29
Hyperactive Member
At roughly 250 nanosecs per Instr or Like, I'm not going to lose
any sleep over the difference.
Either beats the hell out of For, If Mid$(), Next. There used to be
a big arg over FOR-NEXT vs DO-LOOP with DO-LOOP winning but
that was VB3 on 75mz machines.
Last edited by John Yingling; Jul 11th, 2001 at 02:11 AM.
-
Jul 11th, 2001, 02:23 AM
#30
Addicted Member
Using the code above i got the following results.
instr no compare method: 25000001
instr no compare method: 11797
instr binary compare: 25000001
instr binary compare: 390
instr text compare: 25000001
instr text compare: 25096
True
like: 34029
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jul 11th, 2001, 02:51 AM
#31
Registered User
aturner,
That looks very similar to the results I get too.
-
Jul 11th, 2001, 02:52 AM
#32
Addicted Member
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jul 11th, 2001, 03:06 AM
#33
Registered User
Yeah, I guess I was surprised as there seems to be a lot of variation from pc to pc.
How fast is binary compare!
-
Jul 11th, 2001, 10:36 AM
#34
Originally posted by Nucleus
rickm, the text box should have no impact on the relative speed of Instr() vs like.
Out of interest when you ran the code I posted what were your results?
with your code they were tied, with mine, like was about twice as fast.
-
Jul 11th, 2001, 10:39 AM
#35
Originally posted by John Yingling
Right On Nuc,
Textbox.text extracts the contents to temporary string.
That OO. Nothing "operates" on the contents of an object
except through the object's methods. In this case, .text is
a Get property for the contents. Instr and the like never see
the object whence the string came.
Also, for those who get faster times on Like I hope they were not
testing with an Instr(str,"!",vbTextCompare). I'm not sure of the effect of vbTextCompare on Instr even though the search character is not an alpha. Perhaps we could get timings on that. I'm going to loop it right now.
The difference between vbTextCompare and vbBinaryCompare as far as i know has much to do with whether your search is case sensitive or not.
-
Jul 12th, 2001, 09:16 AM
#36
Here are my results for InStr() vs Like. Add it to a Form with a TextBox and a CommandButton. The results will be printed in the debug window.
I have a 500Mhz, and I get results in the 600s for InStr, and 300s for Like.
VB Code:
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Command1_Click()
start = GetTickCount
For i = 1 To 250
'Uncomment the function you would like to use
'If Text1 Like "*!*" Then a = 1
'If InStr(1, Text1, "!") Then a = 1
Next i
pause = GetTickCount
Debug.Print pause - start
End Sub
Private Sub Form_Load()
Text1 = String(30000, "%")
Text1 = Text1 & "!"
Text1 = Text1 & String(25000, "%")
End Sub
-
Jul 12th, 2001, 09:23 AM
#37
Addicted Member
To keep God "i mean megatron" happy i did what he said.
P3 500, 128Mb NT4 sp6 VB6 studio patch 5
Like 491
InStr 1042
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jul 12th, 2001, 09:27 AM
#38
Addicted Member
Megatron
Appologies for being hassled by a newbie...
Why is the loop there??
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jul 12th, 2001, 09:42 AM
#39
lol 
The loop is there so we would loop though the code many times, hence we'll get a more accurate result. 200 times is more percise than just once.
-
Jul 12th, 2001, 09:45 AM
#40
Addicted Member
simple question, simple answer
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
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
|