String Split function incorrect behavior

Yea (found it in the git commits) it was not loop as I thought I had remembered it, it was taking last element:

So basically had to add check if I get something back since having last element is not guarantied due to this bug.

Old — var mapValuesSplit() as String = mapValue.Split(".")

Old— mapValue = mapValuesSplit(mapValuesSplit.Ubound)

New +++ if mapValue.Length > 0 then

New +++ var mapValuesSplit() as String = mapValue.Split(".")

New +++mapValue = mapValuesSplit(mapValuesSplit.Ubound)

New +++ end if

An empty string is a string containing no elements, regardless of the delimiter, and that’s what Split and ToArray return. This is exactly the behavior I would have expected. If you have a bottle crate with no bottles, the number of bottles is zero – it would be quite odd to claim it did contain one empty bottle.

2 Likes

Split has had this behavior for as long as I can remember. I did a quick test in RS2010r5.1 and it returns an empty array - ubound = -1.

Split has had this behaviour probably for as long as the Split function has existed. The oldest version of Xojo that I use is 2016v3, and it behaves the same way. So, as others have mentioned, changing how it works would likely break a lot of code. The one line workaround that I gave above solved the problem for me in all of my applications.

One possible fix, that is compatible, is for Xojo to add an optional boolean parameter to the Split function. If it’s false or missing, then it continues to work the same way as before. If it’s present and set to true, then it always returns an array with at least one element.

every developing language have it quirks.
but we are not speaking about a bottle crate.
its more like a (empty bottle) beside (other empty of filled bottle) and between is a book or not. you have 2 bottles apart or 2 bottles together. this bottles can not disappear.
empty string is a empty bottle somehow.
so “”+"" = “” ← the magic transform into one big empty :slight_smile:
“”+","+"" = “”,""
and a feature request will end by design :wink:

My opinion,
Empty string “” is for me really EMPTY. There is nothing in this string.
So it can not give back anything when splitted (-1 seems correct for me)
String " " is a string including only a space and this will return one element in the array indexed 0.

Before doing the split, perhaps first do a ‘String.IsEmpty’ test.

if you can not split it it stay original.
a cake without using a knife is a cake.

String " " is a string including only a space and this will return one element in the array indexed 0.

“” or " " is equivalent
“,a” or " ,a" the left part is “” or " " and a string

“” != Nil

i know from my office if you ask 10 people all 10 people have a own opinion :exploding_head:

A cake without using a knife is a cake

But without a knife you can’t cut it (divide).

the , is the marker where and the split command is the knife. (in the figurative sense)

somehow off topic but i made a principle: if you can cut all you can join all.
try to cut a paper and hammer down this 2 pieces together, it works.

I disagree.

Also, if you leave off the delimiter, Split returns an array of characters. “” contains no characters, so what should it return? An empty array seems like the correct value.

interesting is that “,” result into 2 entries which is mainly necessary if we process a csv file.

A string with nothing in it is still a string.

Consequently when you split (1,,2) you should have an array with THREE entries.

By the same logic splitting (1) should give an array with ONE entry - whether 1 is “someText” or “”.

So yes, “” is equivalent to " " is equivalent to “a”

1 Like

Where you have the wrong end of the stick is that “” is not nothing but essentially has a NULL character.

One of the biggest breakthroughs in Mathematics was the realisation that 0 is not simply nothing, but a number in its own right. In the same way “” is not nothing but an empty string.

Your view is equivalent to declaring 0 is nothing so we should disregard it and only do Maths with “real” numbers.

In the same way as 0 is not simply nothing but its own number, an empty string it not “nothing” but still a string.

Not at all. That’s quite a stretch of logic.

“” <> chr(0) (null char) <> " " (chr(32)). The first contains zero characters, the other two contain one character each. Standard string comparison will treat them as different from each other.

Stepping back, I will say that there are valid arguments to be made for both results - empty array vs. array with one element, an empty string. But the current implementation goes way back to the beginning of RealBasic. As such, it’s not so much a bug as a feature/change request. This would be a good time to request it, as Xojo is making a lot of changes to functionality.

Honestly, it’s too late already. The change should have been made with API2’s String.FromArray. Now it’s too late.

I think maybe that could be deprecated in favor of a more intuitive .Join method on string arrays, but Xojo has seemed to fall on the side of an empty string being special, so they probably don’t want to change it.

2 Likes

As I mentioned above, it could be changed in such a way that won’t break existing applications.

1 Like

Everyone invariably calls it an EMPTY string, but if an operation is done on it, it shouldn’t be empty !!!

Why not? It’s still an empty string.

You can multiply with 0. The result is 0. Are you saying we shouldn’t do that? It should be -1?

I’m agree, but if you do the code below :

Dim MyTab(-1) as String
MessageBox "'" + String.FromArray(MyTab) + "'"
MyTab.Add ""
MessageBox "'" + String.FromArray(MyTab) + "'"

The result is an empty string ‘’ in both case.
Then what should be the reverse splitting an empty string?

I notice that doing, on an empty array (LastIndex = -1) :
MyTab.IndexOf("re")
return -1.
One solution would be to return an error (Nil objection or OutOfBound or …). As Join an empty Array which could return an error too.
A folderitem which is Nil return an error when we do MyFolderItem.Child(“MyName”).
Then to be clear, an empty Array could be considered as Nil then no mistake of what we expect between working on an empty array / working on an array of empty string.

… isn’t empty.

No. An empty array is still an array.

However if an array has no ENTRIES (aka LastIndex = -1) then it shouldn’t return an empty string.