How Can I Compare Two String Values Regardless of Letter Case?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script that compares two email addresses and tells me whether or not they are the same. Sometimes the email addresses are the same, but the case is different: for example, one address might be example@abc.com and the other might be example@ABC.com. My script always tells me that these are different email addresses. How can I fix this?

— PS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PS. You know, if people knew what the Scripting Guys were really like they’d never trust us with scripting questions like this. For example, a week or so ago one of the Scripting Guys was setting up a family vacation. While making reservations he needed to spell his sister’s married name to the ticket agent. “K as in – “ he said, and then stopped dead in his tracks. For the life of him, this Scripting Guy couldn’t think of a single word that started with the letter K.

So what does that have to do with your question? Nothing. We just thought it was an interesting anecdote.

Ok, but what about your question? As you know, we often tell people not to worry about uppercase and lowercase when it comes to writing scripts. “For the most part VBScript is not case sensitive,” we tell people. “ABC is the same as abc.”

And, in our defense, this is generally true of the keywords, functions, statements and other goodies that make up VBScript. For example, this line of code – odd as it might look – will pop up a message box, no problem:

mSGboX “This is a message box.”

However, just because you can type Msgbox any way you want doesn’t mean that the procedures and tests VBScript carries out are also case-insensitive. For example, try running this script, which compares example@abc.com and example@ABC.com:

str1 = “example@abc.com”
str2 = “example@ABC.com”

If str1 = str2 Then Wscript.Echo “The strings are equal.” Else Wscript.Echo “The strings are not equal.” End If

Run the script, and you’ll get this message:

The strings are not equal.

Why? Well, by default, when VBScript compares string values it compares the ASCII values of each character in the string. In the wonderful world of ASCII, the uppercase A and the lowercase a have different values (65 and 97, respectively). Because the ASCII values are different VBScript insists that the two strings are different.

So how can we work around this issue? We’re going to show you two different solutions.

For starters, you can use the VBScript function StrComp (for string comparison); this helps ensure that your script does a text comparison rather than a binary comparison. (A binary comparison sees A and a as different characters; a text comparison does not.) For example:

str1 = “example@abc.com”
str2 = “example@ABC.com”

intCompare = StrComp(str1, str2, vbTextCompare)

If intCompare = 0 Then Wscript.Echo “The strings are equal.” Else Wscript.Echo “The strings are not equal.” End If

In this script we specify our two string values (str1 and str2) and then call the StrComp function. StrComp gets three parameters: the two strings being compared and the VBScript constant vbTextCompare. The results of the string comparison are then stored in a variable we named intCompare. If intCompare equals 0 then the two strings are equal; if intCompare is equal to anything but 0, then the two strings are different.

Give it a try and see what happens. You should get back this message:

The strings are equal.

Whew. Much better.

Here’s another way to ensure that strings get compared without worrying about letter case. In this script we use the UCase function to convert all the letters in the two strings to uppercase (in other words, both strings will be converted to EXAMPLE@ABC.COM). Because there are no lowercase letters to worry about this script will report back that the two strings are the same:

str1 = UCase(“example@abc.com”)
str2 = UCase(“example@ABC.com”)

If str1 = str2 Then Wscript.Echo “The strings are equal.” Else Wscript.Echo “The strings are not equal.” End If

Remember, you should use one of these techniques only if case doesn’t matter. If case does matter (that is, if example@ABC.com should not be considered identical to example@abc.com), then just use a regular old equivalence test:

str1 = “example@abc.com”
str2 = “example@ABC.com”

If str1 = str2 Then Wscript.Echo “The strings are equal.” Else Wscript.Echo “The strings are not equal.” End If

And in case you’re wondering, our Scripting Guy eventually did come up with “Kansas” as a word that started with the letter K. Good for him!

0 comments

Discussion is closed.

Feedback usabilla icon