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

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

  • Comments 7
  • Likes
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!

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks, this was very helpful.

  • What a cool and clear explanation, even a greenhorn like me can understand this, thx ScriptingGuy1!

  • Hyper, it's even easier in PowerShell. ("string" -eq "StRiNg") will return true...well specifically, $true.

    VBScript is a legacy scripting language. All new development is being done within PowerShell. You'll have to learn it sooner or later if you want to script, so don't bother with VBScript.

  • Thanks! Your explanation is very clear and the examples are very simple to understand.

  • Hi, This was really helpful.I have a question.

    I am capturing the senders email address from my VBscript through

    Dim myolApp As Outlook.Application

       Dim myNamespace As Outlook.NameSpace

       Set myolApp = CreateObject("Outlook.Application")

       Set myNamespace = myolApp.GetNamespace("MAPI")

       MsgBox myNamespace.CurrentUser.AddressEntry

    Now I want this email address to be checked against a file that contains a list of email addresses.Only if this email is already registered there then proceed otherwise send an  error message to the sender.I am not sure how to check if this email is is there.

  • Just what I was looking for!!! Thanks

  • Awesome Explanation !!!