+1 vote

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

Output:

[]
False

maybe someone can shed some light on this issue?

Thanks

in Engine by (13 points)

This actually prints an empty line.

var nstr = ""
print(nstr) #will not print! why?

1 Answer

+1 vote

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.

by (3,445 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.