A way to get "Like" to use "Option Compare Text" without changing the setting for all my code?

L

Linda

Hi,

Is there a way to do a "text" (rather than "binary") compareison with
the "like" operator, without changing the global "Option Compare"
setting? I don't want to risk breaking many, many lines of functional
code just to get one "like" operation to behave as I wish.

I want to check whether a single-character string is (a letter or
number, INCLUDING diacritical letters) or whether it is (something
else.)

if str1$ like "[A-Z]" or str1$ like "#" then
'blah blah blah
else
'blah blah blah
end if

....would work if I changed the source file's option to "text", but as I
said, I don't want to risk breaking the rest of the code. It's the need
to include diacritical letters in that makes this complicated,
otherwise I could just add an [a-z] comparison.

Is there another way to do this, or a way to make this work?

Thanks.
 
L

Linda

In case anyone is interested / faces a similar problem, here's how I
solved it:

In the library that I use for most of my applications, I created a
"TextCompare" class in it's own source file, which is Option Compare
Text. The class consists of public shared functions that simply invoke
the "like" operator and other comparison checks that reply on "Option
Compare".

(For my other source files, I've added an explicit "Option Compare
Binary" in addition to the obligitory "Option Strict On" and "Option
Explicit On" just so it's unambigous what's happening).

Now, if I want "Compare Binary" I do the comparison the standard way.
If I want "Compare Text" (and String.Compare() isn't appropriate) I
call the TextCompare static method.

=======================================================

Option Strict On
Option Explicit On
Option Compare Text

Public Class TextCompare

Public Shared Function txtEquals(ByVal str1$, ByVal str2$) _
As Boolean
Return str1$.Equals(str2$)
End Function

Public Shared Function txtLike(ByVal str1$, ByVal pattern$) _
As Boolean
If str1$ Like pattern$ Then Return True
Return False
End Function

Public Shared Function indexOf(ByVal outerStr$, ByVal innerStr$) _
As Integer
Return outerStr$.IndexOf(innerStr$)
End Function

Public Shared Function lastIndexOf(ByVal outerStr$, ByVal
innerStr$) _
As Integer
Return outerStr$.LastIndexOf(innerStr)
End Function

Public Shared Function startsWith(ByVal outerStr$, ByVal innerStr$)
_
As Boolean
Return outerStr$.StartsWith(innerStr)
End Function

Public Shared Function endsWIth(ByVal outerStr$, ByVal innerStr$) _
As Boolean
Return outerStr$.EndsWith(innerStr)
End Function

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top