Results 1 to 9 of 9

Thread: Like & InStr

  1. #1
    WorkHorse
    Guest

    Like & InStr

    Is there a way to combine the Like operator and the InStr function? I want to use the wildcards available with Like to search a string and get the location of the found text like InStr.

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    If Instr("SDF") Like ("F") then Msgbox "Foudn it"
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3
    WorkHorse
    Guest
    That doesn't work at all. And, it would only return True/False. I need to get the location of the found text like with InStr.

    For example:

    I want to search a string for the word "Department" but only when not followed by the word "of".

    VB Code:
    1. lFound = sSearchStr Like "*Department?[!o][!f][! ]*"
    returns True or False. InStr returns the location of the found SSearchStr:

    VB Code:
    1. lFound = InStr(1, sSearchStr, "Department")
    But I want something like:

    VB Code:
    1. lFound = InStr(1, sSearchStr, Like "*Department?[!o][!f][! ]*")
    But that, of course, doesn't work.

  4. #4
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Well maybe if you had explained what it was you wanted in the first place......! you post vauge questions you get vauge answers.


    I'll see what i can do.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    WorkHorse
    Guest
    Sorry. I thought "get the location of the found text like InStr" would make it clear. I mean I want a number representing the location of the text that a Like operator finds (like the number returned from InStr).

    Thanks for looking at this Arc. Any help would be greatly appreciated.

  6. #6
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    I forgot i dont even have VB installe don here yet... just reformated.


    But basically you would use the Split Function.

    VB Code:
    1. Dim iIndex as integer,Found as Integer
    2.  
    3. MyArray=Split(Mystring," ")
    4.  
    5. For i = 1 to Ubound(MYarray)
    6. If Myarray(i) = "Department" Then
    7.  iIndex=i
    8. Found=1
    9. end if
    10.  
    11. If Myarray(i) ="of" And i = iIndex+1 And Found = 1 Then
    12. 'The Word "of" was after the word "department"
    13. End if

    Or something like that.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  7. #7
    WorkHorse
    Guest
    Well, OK. I guess I should have mentioned that I need to go through big strings (Word documents) very fast.

    I'd need to do about 3000 Find searches, and Word's Find function isn't nearly as fast as code. So I'm trying to do a simple replecation the Find function in code (with the docuemnt I'm working with it usually won't find anything, but when it does I just need the location of the found text to run the rest of the code).

    (EDIT: Also I can't just check for one word (like "of") because the database of search criteria uses wildcard formats.)

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Workhorse
    Here is a little throw together that sorta combines instr and like in a sub. U cant get a 'position' from the like function because by its very nature the whole strings match if Like returns true.

    I am not sure how fast this method is and u may find that Instr and Like are quite slow.
    Regards
    Stuart
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim SearchString As String
    3.     Dim SearchLength As String
    4.     Dim PriorString As String
    5.     Dim PriorLength As Integer
    6.     Dim FoundPosn As Integer
    7.     Dim FinalPosn As Integer
    8.    
    9.     SearchString = "This is the story of Department life in a Department"
    10.     SearchLength = Len(SearchString)
    11.     PriorString = "of "
    12.     PriorLength = Len(PriorString)
    13.  
    14.     FoundPosn = InStr(1, SearchString, "Department")
    15.     Do While FoundPosn > 0
    16.         If FoundPosn >= PriorLength And Mid$(SearchString, FoundPosn - PriorLength, PriorLength) Like PriorString Then
    17.             FoundPosn = InStr(FoundPosn + 1, SearchString, "Department")
    18.         Else
    19.             FinalPosn = FoundPosn
    20.             FoundPosn = 0
    21.         End If
    22.     Loop
    23.            
    24.     Debug.Print FinalPosn
    25. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    New Member
    Join Date
    Feb 2023
    Posts
    1

    Re: Like & InStr

    Or even more simply use a function like InStrW:

    Function InStrW(Src As String, Pattern As String) As Integer
    InStrW = 0
    For i = 1 To Len(Src)
    If Mid(Src, i) Like Pattern & "*" Then InStrW = i + Len(Pattern): Exit For
    Next i
    End Function

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