{"id":343,"date":"2006-12-04T04:00:00","date_gmt":"2006-12-04T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/12\/04\/comparing-apples-to-oranges\/"},"modified":"2006-12-04T04:00:00","modified_gmt":"2006-12-04T12:00:00","slug":"comparing-apples-to-oranges","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/12\/04\/comparing-apples-to-oranges\/","title":{"rendered":"Comparing Apples to Oranges"},"content":{"rendered":"<p>I&#8217;ve spent some time lately exploring searching and replacing techniques in a variety of scripting languages.&nbsp; I hope to share some of these experiences later. Right now I wanted to briefly share a tip when searching for text using VBScript.&nbsp; This is a common task and one that causes many novice scripters more than a little grief.<\/p>\n<p>A typical VBScript expression is to use InStr() to determine whether a given substring exists within a larger string. In a related vein, you might use the Replace function to replace all instances of a given string with another. The problem is how you accomplish that comparison.&nbsp; By default both functions perform a binary comparison. This means that &#8220;APPLE&#8221; and &#8220;APPLE&#8221; will match, but &#8220;APPLE&#8221; and &#8220;Apple&#8221; won&#8217;t. What you need to do is perform a text comparison which ignores case.&nbsp; <\/p>\n<p>Both InStr and Replace have an optional parameter to specify the comparison method. You can use the VBScript constants of <span style=\"font-family: Courier New,Courier,mono;\">vbTextCompare <\/span>or <span style=\"font-family: Courier New,Courier,mono;\">vbBinaryCompare<\/span>.&nbsp; If you use the former, I strongly recommend you use the complete syntax for each function. In fact, you&#8217;ll likely get an error if you don&#8217;t.<\/p>\n<p>I&#8217;ve put together a sample script that demonstrates how the different techniques work.<\/p>\n<p><span style=\"font-family: Courier New,Courier,mono;\">s=&#8221;The quick brown fox used Primalscript. The fox was smart!&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8216;FOX (or any other case formatting such as fOX) only works with vbTextCompare<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8216;fox works in all cases since it is an exact match<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">strFind=&#8221;FOX&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">WScript.Echo &#8220;Using simple defaults&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">If InStr(s,strFind) Then <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo &#8220;Found with using defaults&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; s2=Replace(s,strFind,&#8221;dog&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo s2<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Else<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo &#8220;Not found with defaults&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">End If<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">WScript.Echo String(20,&#8221;*&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">WScript.Echo &#8220;Using vbBinaryCompare&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">If InStr(1,s,strFind,vbBinarycompare) Then <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo &#8220;Found with vbBinaryCompare&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; s2=Replace(s,strFind,&#8221;dog&#8221;,1,-1,vbBinaryCompare)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo s2<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Else<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo &#8220;Not found with vbBinaryCompare&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">End If<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">WScript.Echo String(20,&#8221;*&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">WScript.Echo &#8220;Using vbTextCompare&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">If InStr(1,s,strFind,vbTextcompare) Then <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo &#8220;Found with vbTextCompare&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; s2=Replace(s,strFind,&#8221;dog&#8221;,1,-1,vbTextCompare)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo s2<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Else<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo &#8220;Not found with vbTextCompare&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">End If<\/span><\/p>\n<p>Save this code to your computer. Run it with Cscript at a command prompt. Play around with different variations for strFind and you&#8217;ll quickly appreciate the differences. I&#8217;m not advocating you never use <span style=\"font-family: Courier New,Courier,mono;\">vbBinaryCompare <\/span>or the default settings for InStr and Replace. I merely want you to understand the differences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve spent some time lately exploring searching and replacing techniques in a variety of scripting languages.  I hope to share some of these experiences later. Right now I wanted to briefly share a tip when searching for text using VBScript.  This is a common task and one that causes many novice scripters more than a little grief.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-343","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/343","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=343"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/343\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}