Can you turn off case-sensitivity in VBScript strings?
Solution 1
There is a StrComp
function which allows performing a case-insensitive comparison of two strings by passing vbTextCompare
as the third argument. The main documentation doesn't make that obvious, but it is discussed in this Hey, Scripting Guy article.
For example:
If StrComp(strFoo, Request.QueryString("x"), vbTextCompare) = 0 Then ...
However, in practice, I use LCase
or UCase
way more than StrComp
for case-insensitive string comparisons because it's more obvious to me.
Solution 2
No. Depending on the function the option may be there (InStr for example) as an optional parameter, but for just straight comparison, there is no global option.
One little known option that can be handy is if you have a list of strings and you want to see if a string is in that list:
Dim dicList : Set dicList = CreateObject("Scripting.Dictionary")
Dim strTest
dicList.CompareMode = 0 ' Binary ie case sensitive
dicList.Add "FOO", ""
dicList.Add "BAR", ""
dicList.Add "Wombat", ""
strTest = "foo"
WScript.Echo CStr(dicList.Exists(strTest))
Set dicList = CreateObject("Scripting.Dictionary")
dicList.CompareMode = 1 ' Text ie case insensitive
dicList.Add "FOO", ""
dicList.Add "BAR", ""
dicList.Add "Wombat", ""
strTest = "foo"
WScript.Echo CStr(dicList.Exists(strTest))
Solution 3
I doubt the existence of such an option since if there were something like that and you use it, you'll lose the ability to compare strings in a case sensitive manner.
Dan W
Updated on June 05, 2022Comments
-
Dan W almost 2 years
I'm pretty sure the answer to this is no. I know that I can write
if lcase(strFoo) = lcase(request.querystring("x")) then...
or use inStr, but I just want to check there isn't some undocumented setting buried in the registry or somewhere that makes the content of VBScript strings behave consistently with the rest of the scripting language!
Thanks Dan
-
JohnB over 13 yearspass in
0
for vbTextCompare (or leave the 3rd argument out) for binary (case sensitive) compare; pass in1
for textual (case insensitive) compare