{"id":69543,"date":"2005-06-22T17:48:00","date_gmt":"2005-06-22T17:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/06\/22\/how-can-i-compare-two-string-values-regardless-of-letter-case\/"},"modified":"2005-06-22T17:48:00","modified_gmt":"2005-06-22T17:48:00","slug":"how-can-i-compare-two-string-values-regardless-of-letter-case","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-compare-two-string-values-regardless-of-letter-case\/","title":{"rendered":"How Can I Compare Two String Values Regardless of Letter Case?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>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 <I>are<\/I> the same, but the case is different: for example, one address might be <I>example@abc.com<\/I> and the other might be <I>example@ABC.com<\/I>. My script always tells me that these are <I>different<\/I> email addresses. How can I fix this?<BR><BR>&#8212; PS<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, PS. You know, if people knew what the Scripting Guys were <I>really<\/I> like they\u2019d 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\u2019s married name to the ticket agent. \u201cK as in &#8211; \u201c he said, and then stopped dead in his tracks. For the life of him, this Scripting Guy couldn\u2019t think of a single word that started with the letter <I>K<\/I>.<\/P>\n<P>So what does that have to do with your question? Nothing. We just thought it was an interesting anecdote.<\/P>\n<P>Ok, but what <I>about<\/I> your question? As you know, we often tell people not to worry about uppercase and lowercase when it comes to writing scripts. \u201cFor the most part VBScript is <I>not<\/I> case sensitive,\u201d we tell people. \u201c<I>ABC<\/I> is the same as <I>abc<\/I>.\u201d<\/P>\n<P>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 &#8211; odd as it might look &#8211; will pop up a message box, no problem:<\/P><PRE class=\"codeSample\">mSGboX &#8220;This is a message box.&#8221;\n<\/PRE>\n<P>However, just because you can type <B>Msgbox<\/B> any way you want doesn\u2019t 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:<\/P><PRE class=\"codeSample\">str1 = &#8220;example@abc.com&#8221;\nstr2 = &#8220;example@ABC.com&#8221;<\/p>\n<p>If str1 = str2 Then\n    Wscript.Echo &#8220;The strings are equal.&#8221;\nElse\n    Wscript.Echo &#8220;The strings are not equal.&#8221;\nEnd If\n<\/PRE>\n<P>Run the script, and you\u2019ll get this message:<\/P><PRE class=\"codeSample\">The strings are not equal.\n<\/PRE>\n<P>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 <I>A<\/I> and the lowercase <I>a<\/I> have different values (65 and 97, respectively). Because the ASCII values are different VBScript insists that the two strings are different.<\/P>\n<P>So how can we work around this issue? We\u2019re going to show you two different solutions.<\/P>\n<P>For starters, you can use the VBScript function<B> StrComp<\/B> (for <B>string comparison<\/B>); this helps ensure that your script does a text comparison rather than a binary comparison. (A binary comparison sees <I>A<\/I> and <I>a<\/I> as different characters; a text comparison does not.) For example:<\/P><PRE class=\"codeSample\">str1 = &#8220;example@abc.com&#8221;\nstr2 = &#8220;example@ABC.com&#8221;<\/p>\n<p>intCompare = StrComp(str1, str2, vbTextCompare)<\/p>\n<p>If intCompare = 0 Then\n    Wscript.Echo &#8220;The strings are equal.&#8221;\nElse\n    Wscript.Echo &#8220;The strings are not equal.&#8221;\nEnd If\n<\/PRE>\n<P>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 <I>but<\/I> 0, then the two strings are different.<\/P>\n<P>Give it a try and see what happens. You should get back this message:<\/P><PRE class=\"codeSample\">The strings are equal.\n<\/PRE>\n<P>Whew. Much better.<\/P>\n<P>Here\u2019s another way to ensure that strings get compared without worrying about letter case. In this script we use the <B>UCase<\/B> function to convert all the letters in the two strings to uppercase (in other words, both strings will be converted to <I>EXAMPLE@ABC.COM<\/I>). Because there are no lowercase letters to worry about this script will report back that the two strings are the same:<\/P><PRE class=\"codeSample\">str1 = UCase(&#8220;example@abc.com&#8221;)\nstr2 = UCase(&#8220;example@ABC.com&#8221;)<\/p>\n<p>If str1 = str2 Then\n    Wscript.Echo &#8220;The strings are equal.&#8221;\nElse\n    Wscript.Echo &#8220;The strings are not equal.&#8221;\nEnd If\n<\/PRE>\n<P>Remember, you should use one of these techniques only if case doesn\u2019t matter. If case <I>does<\/I> matter (that is, if example@ABC.com should not be considered identical to example@abc.com), then just use a regular old equivalence test:<\/P><PRE class=\"codeSample\">str1 = &#8220;example@abc.com&#8221;\nstr2 = &#8220;example@ABC.com&#8221;<\/p>\n<p>If str1 = str2 Then\n    Wscript.Echo &#8220;The strings are equal.&#8221;\nElse\n    Wscript.Echo &#8220;The strings are not equal.&#8221;\nEnd If\n<\/PRE>\n<P>And in case you\u2019re wondering, our Scripting Guy eventually <I>did<\/I> come up with \u201cKansas\u201d as a word that started with the letter <I>K<\/I>. Good for him!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,21,5],"class_list":["post-69543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-string-manipulation","tag-vbscript"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69543\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}