{"id":1898,"date":"2015-09-15T18:00:04","date_gmt":"2015-09-15T17:00:04","guid":{"rendered":"http:\/\/www.unicoda.com\/?p=1898"},"modified":"2015-09-15T14:47:56","modified_gmt":"2015-09-15T13:47:56","slug":"classic-asp-improving-split-function","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=1898","title":{"rendered":"[Classic ASP] Improving split function"},"content":{"rendered":"<p>Coming from the JavaScript world, I&rsquo;m used to split function working as follow :<\/p>\n<pre>\"\".split(\",\")\r\n&gt; Array [ \"\" ]<\/pre>\n<p>It&rsquo;s basic JavaScript. Take an empty string, try to split it on a delimiter, it will return an array containing just one element : an empty string. It&rsquo;s exactly what you expect from the function, always return an array. Like it should be in every programming language.<\/p>\n<p>A few weeks ago, I had to deal with split function in ASP Classic. Calling the function is a bit different, but that&rsquo;s not a big deal :<\/p>\n<pre>split(\"foo,bar\", \",\")<\/pre>\n<p>The function works fine in almost every cases. But it has an unexpected behaviour when you use it on an empty string. Let&rsquo;s try it :<\/p>\n<pre>Dim array\r\narray = split(\"\", \",\")\r\n'-- Expect array to contain one element\r\nUBound(array) '-- Returns -1. Wait! What?\r\narray(0)      '-- Guess what: Internal server error<\/pre>\n<p>That&rsquo;s a problem. The split function does not return an array containing an empty string when you try to split an empty string, it just returns an array with nothing in it &#8211; hence UBound returning -1.<\/p>\n<p>When you are aware of this particularity, it&rsquo;s easier to write your code. Since I personally prefer a JavaScript-like split function, I decided to write a betterSplit function doing the job as I wanted :<\/p>\n<pre>'-- Improve Split to work as expected\r\nFunction betterSplit(str, delimiter)\r\n\u00a0 Dim ar\r\n\u00a0 ar = Split(str, delimiter)\r\n\r\n\u00a0 '-- UBound\r\n\u00a0 '-- &lt; 0 if empty string\r\n\u00a0 '-- = 0 if no delimiter in string\r\n\u00a0 If UBound(ar) &lt;= 0 Then\r\n\u00a0\u00a0\u00a0 ReDim ar(0)\r\n\u00a0\u00a0\u00a0 ar(0) = str\r\n\u00a0 End If\r\n\r\n\u00a0 betterSplit = ar\r\nEnd Function<\/pre>\n<p>As a matter of fact, I find this function much better than the original split function as it always returns an array even when using an empty string as a parameter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Coming from the JavaScript world, I&rsquo;m used to split function working as follow : \u00ab\u00a0\u00a0\u00bb.split(\u00ab\u00a0,\u00a0\u00bb) &gt; Array [ \u00ab\u00a0\u00a0\u00bb ] It&rsquo;s basic JavaScript. Take an empty string, try to split it on a delimiter, it will return an array containing just one element : an empty string. It&rsquo;s exactly what you expect from the function, &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=1898\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;[Classic ASP] Improving split function&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[250,251],"class_list":["post-1898","post","type-post","status-publish","format-standard","hentry","category-code","tag-asp","tag-split"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1898","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1898"}],"version-history":[{"count":2,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1898\/revisions"}],"predecessor-version":[{"id":1907,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1898\/revisions\/1907"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}