-
Jan 19th, 2002, 12:29 AM
#1
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.
-
Jan 19th, 2002, 01:02 AM
#2
PowerPoster
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.
-
Jan 19th, 2002, 01:16 AM
#3
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:
lFound = sSearchStr Like "*Department?[!o][!f][! ]*"
returns True or False. InStr returns the location of the found SSearchStr:
VB Code:
lFound = InStr(1, sSearchStr, "Department")
But I want something like:
VB Code:
lFound = InStr(1, sSearchStr, Like "*Department?[!o][!f][! ]*")
But that, of course, doesn't work.
-
Jan 19th, 2002, 01:21 AM
#4
PowerPoster
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.
-
Jan 19th, 2002, 01:31 AM
#5
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.
-
Jan 19th, 2002, 01:31 AM
#6
PowerPoster
I forgot i dont even have VB installe don here yet... just reformated.
But basically you would use the Split Function.
VB Code:
Dim iIndex as integer,Found as Integer
MyArray=Split(Mystring," ")
For i = 1 to Ubound(MYarray)
If Myarray(i) = "Department" Then
iIndex=i
Found=1
end if
If Myarray(i) ="of" And i = iIndex+1 And Found = 1 Then
'The Word "of" was after the word "department"
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.
-
Jan 19th, 2002, 01:40 AM
#7
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.)
-
Jan 19th, 2002, 03:41 AM
#8
PowerPoster
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:
Private Sub Command1_Click()
Dim SearchString As String
Dim SearchLength As String
Dim PriorString As String
Dim PriorLength As Integer
Dim FoundPosn As Integer
Dim FinalPosn As Integer
SearchString = "This is the story of Department life in a Department"
SearchLength = Len(SearchString)
PriorString = "of "
PriorLength = Len(PriorString)
FoundPosn = InStr(1, SearchString, "Department")
Do While FoundPosn > 0
If FoundPosn >= PriorLength And Mid$(SearchString, FoundPosn - PriorLength, PriorLength) Like PriorString Then
FoundPosn = InStr(FoundPosn + 1, SearchString, "Department")
Else
FinalPosn = FoundPosn
FoundPosn = 0
End If
Loop
Debug.Print FinalPosn
End Sub
-
Feb 5th, 2023, 12:41 PM
#9
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|