Topic was automatically imported from the old Question2Answer platform.
Asked By
weirdo23
calling split on an empty string returns a PoolStringArray that will claim not to be empty while clearly being empty.
here is my code example:
var nstr = ""
print(nstr) #will not print! why?
var strarr = nstr.split(" ",true,0)
print(strarr) #prints [] an empty array
print(strarr.empty()) #prints "False", claims to have contents
Hi!
You passed to the function allow_empty as true. This means that the PoolStringArray will have one element with the emtpy character (“” is represented by 0x00 in a string), as you are allowing for that. if you use var strarr = nstr.split(" ",false,0) you will notice it will return an empty array.
For checking that is different that an array has an empty character, and an empty array, you can do this:
var arr = PoolStringArray([])
var arr2 = PoolStringArray([""])
print(arr.size()) #prints 0
print(arr2.size()) #prints 1
print(arr.empty()) #prints true
print(arr2.empty()) #prints false
In summpary, with allow_true you are allowing empty string to be set as component of the resulting array, or at least thats what i always understood, since it seems not to be explained in documentation.