-
May 14th, 2021, 02:31 AM
#1
Thread Starter
PowerPoster
[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
-
May 14th, 2021, 02:48 AM
#2
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
-
May 14th, 2021, 02:50 AM
#3
Thread Starter
PowerPoster
Re: Instr search
Originally Posted by Arnoutdv
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?
-
May 14th, 2021, 03:03 AM
#4
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
-
May 14th, 2021, 03:07 AM
#5
Re: Instr search
Originally Posted by luca90
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.
-
May 14th, 2021, 08:02 AM
#6
Re: Instr search
Originally Posted by some1uk03
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.
-
May 14th, 2021, 11:38 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|