Results 1 to 7 of 7

Thread: [RESOLVED] Instr search

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Resolved [RESOLVED] Instr search

    In need to find in mystring the exactlly value, not the part...

    Mystring="aaaareeeezzzzz"

    if value to find is mySerch="zz" return=false, if mySerch="zzzzz" return=true

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Instr search

    That's not possible using InStr()
    "zz" is part of "zzzzz"
    You will have to write your own sub string locator.
    Build a loop around the InStr() for searching "zz".
    If match then check the character before and the character after.
    If OK then return True else repeat the InStr starting a the previous found position + 1

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: Instr search

    Quote Originally Posted by Arnoutdv View Post
    That's not possible using InStr()
    "zz" is part of "zzzzz"
    You will have to write your own sub string locator.
    Build a loop around the InStr() for searching "zz".
    If match then check the character before and the character after.
    If OK then return True else repeat the InStr starting a the previous found position + 1
    other way to find perfect word in string?

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: Instr search

    Not built in for the exact sample you gave

    If you want to find a word then I assume there is also white space in the text?

    If it is a sentence then you could use something like this:
    Code:
    Private Function FindWord(ByVal sText As String, ByVal sWord As String) As Boolean
      FindWord = InStr(1, " " & sText & " ", " " & sWord & " ") > 0 
    End Function

  5. #5
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,663

    Re: Instr search

    Quote Originally Posted by luca90 View Post
    In need to find in mystring the exactlly value, not the part...

    Mystring="aaaareeeezzzzz"

    if value to find is mySerch="zz" return=false, if mySerch="zzzzz" return=true
    Something along the lines of:
    Code:
    if instr(1,Mystring,"zz") = len("zz") then
      msgbox "Bingo!", vbOkonly
    End if
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Instr search

    Quote Originally Posted by some1uk03 View Post
    Something along the lines of:
    Code:
    if instr(1,Mystring,"zz") = len("zz") then
      msgbox "Bingo!", vbOkonly
    End if
    Nope that definitely will not work. It would return true only if it found zz at position 2 of the string. So it would be true if the source string was "aazzzzzzzzzbb...." since it would find zz at position 2.

    If you want to find an exact match then you must check the characters on each side.

  7. #7
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [RESOLVED] Instr search

    Another approach:

    Code:
    Option Explicit
    Option Compare Binary 'But already the default anyway.
    
    Private Function ExactlyIn( _
        ByRef Searched As String, _
        ByRef Pattern As String, _
        Optional ByRef Bracket As String = vbNullChar) As Boolean
        'Bracket is some character that never occurs in the other arguments.
        ExactlyIn = Bracket & Searched & Bracket _
               Like "*[!" & Left$(Pattern, 1) & "]" _
                  & Pattern _
                  & "[!" & Right$(Pattern, 1) & "]*"
    End Function
    
    Private Sub Form_Load()
        Dim MyString As String
    
        MyString = "aaaareeeezzzzz"
        AutoRedraw = True 'Show the printing.
        Print ExactlyIn(MyString, "zz")
        Print ExactlyIn(MyString, "zzzz")
        Print ExactlyIn(MyString, "zzzzz")
        Print ExactlyIn(MyString, "zzzzzz")
    End Sub

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