By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,579 Members | 1,670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,579 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.