Page 1 of 2 12 LastLast
Results 1 to 40 of 55

Thread: Find a ! in a string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344

    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)

  2. #2
    Tygur
    Guest
    If InStr(StringName, "!") > 0 Then
    MsgBox "It's In there!"
    End If

  3. #3
    Lively Member Pete Rosborough's Avatar
    Join Date
    May 2001
    Location
    Central NY
    Posts
    68
    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Posts
    344
    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)

  5. #5
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    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

  6. #6
    Matthew Gates
    Guest
    You could also use the Like operator.


    Code:
    If StringName Like "*!*" Then Msgbox "StringName contains '!'"

  7. #7
    Tygur
    Guest
    If it matters, InStr is much faster than Like.

  8. #8
    Matthew Gates
    Guest
    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 .

  9. #9
    Tygur
    Guest
    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.

  10. #10
    Megatron
    Guest
    While Like may be slightly slower, it's the prefered method for pattern matching, whereas InStr is prefered for searching.

  11. #11
    Tygur
    Guest
    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.

  12. #12
    Matthew Gates
    Guest
    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.

  13. #13
    Tygur
    Guest
    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.

  14. #14
    rickm
    Guest
    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

  15. #15
    Megatron
    Guest
    Originally posted by Tygur
    and I know it's faster, so I prefer InStr.
    Like is almost twice as fast as InStr

  16. #16
    rickm
    Guest
    Originally posted by Megatron


    Like is almost twice as fast as InStr
    I get the same result

  17. #17
    Tygur
    Guest
    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.

  18. #18
    rickm
    Guest
    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.

  19. #19
    Member IUnknown's Avatar
    Join Date
    May 2001
    Location
    Staffordshire, England
    Posts
    59
    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

  20. #20
    rickm
    Guest
    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.

  21. #21
    Tygur
    Guest
    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.

  22. #22
    rickm
    Guest
    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.

  23. #23
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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:
    1. Private Declare Function GetTickCount Lib "kernel32" () As Long
    2.  
    3. Private Sub Command1_Click()
    4. Dim start As Double, test As String
    5. test = test & String(25000000, "7")
    6. test = test & "!"
    7.  
    8. start = GetTickCount
    9. Debug.Print "instr: " & InStr(1, test, "!")
    10. Debug.Print "instr: " & GetTickCount - start
    11.  
    12. start = GetTickCount
    13. Debug.Print "like: " & test Like "*!*"
    14. Debug.Print "like: " & GetTickCount - start
    15.  
    16. End Sub

    Run on a PIII

  24. #24
    rickm
    Guest
    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:
    1. Private Declare Function GetTickCount Lib "kernel32" () As Long
    2.  
    3. Private Sub Command1_Click()
    4. Dim start As Double, test As String
    5. test = test & String(25000000, "7")
    6. test = test & "!"
    7.  
    8. start = GetTickCount
    9. Debug.Print "instr: " & InStr(1, test, "!")
    10. Debug.Print "instr: " & GetTickCount - start
    11.  
    12. start = GetTickCount
    13. Debug.Print "like: " & test Like "*!*"
    14. Debug.Print "like: " & GetTickCount - start
    15.  
    16. 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

  25. #25
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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?

  26. #26
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    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.

  27. #27
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    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.

  28. #28
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4.  
    5. Private Sub Command1_Click()
    6. Dim start As Double, test As String
    7. test = test & String(25000000, "7")
    8. test = test & "!"
    9.  
    10. start = GetTickCount
    11. Debug.Print "instr no compare method: " & InStr(1, test, "!")
    12. Debug.Print "instr no compare method: " & GetTickCount - start
    13.  
    14. start = GetTickCount
    15. Debug.Print "instr binary compare: " & InStr(1, test, "!", vbBinaryCompare)
    16. Debug.Print "instr binary compare: " & GetTickCount - start
    17.  
    18. start = GetTickCount
    19. Debug.Print "instr text compare: " & InStr(1, test, "!", vbTextCompare)
    20. Debug.Print "instr text compare: " & GetTickCount - start
    21.  
    22. start = GetTickCount
    23. Debug.Print "like: " & test Like "*!*"
    24. Debug.Print "like: " & GetTickCount - start
    25.  
    26. End Sub

  29. #29
    Hyperactive Member
    Join Date
    Apr 2001
    Posts
    315
    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.

  30. #30
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    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

  31. #31
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    aturner,

    That looks very similar to the results I get too.

  32. #32
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    well it was your code
    Due to the energy crisis, the light at the end of the tunnel has been turned off.
    Sorry for any inconvenience this may cause

  33. #33
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Yeah, I guess I was surprised as there seems to be a lot of variation from pc to pc.

    How fast is binary compare!

  34. #34
    rickm
    Guest
    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.

  35. #35
    rickm
    Guest
    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.

  36. #36
    Megatron
    Guest
    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:
    1. Private Declare Function GetTickCount Lib "kernel32" () As Long
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     start = GetTickCount
    6.    
    7.     For i = 1 To 250
    8.         'Uncomment the function you would like to use
    9.         'If Text1 Like "*!*" Then a = 1
    10.         'If InStr(1, Text1, "!") Then a = 1
    11.     Next i
    12.    
    13.     pause = GetTickCount
    14.    
    15.     Debug.Print pause - start
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     Text1 = String(30000, "%")
    20.     Text1 = Text1 & "!"
    21.     Text1 = Text1 & String(25000, "%")
    22. End Sub

  37. #37
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    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

  38. #38
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    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

  39. #39
    Megatron
    Guest
    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.

  40. #40
    Addicted Member aturner's Avatar
    Join Date
    Nov 2000
    Posts
    179
    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

Page 1 of 2 12 LastLast

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