"".split(",") > Array [ "" ]
A few weeks ago, I had to deal with split function in ASP Classic. Calling the function is a bit different, but that’s not a big deal :
The function works fine in almost every cases. But it has an unexpected behaviour when you use it on an empty string. Let’s try it :
Dim array array = split("", ",") '-- Expect array to contain one element UBound(array) '-- Returns -1. Wait! What? array(0) '-- Guess what: Internal server error
That’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 – hence UBound returning -1.
'-- Improve Split to work as expected Function betterSplit(str, delimiter) Dim ar ar = Split(str, delimiter) '-- UBound '-- < 0 if empty string '-- = 0 if no delimiter in string If UBound(ar) <= 0 Then ReDim ar(0) ar(0) = str End If betterSplit = ar End Function
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.