"If myTextbox
was empty, what would be the length of myArray
"?
Check the docs under String's split
option:
"If the delimiter parameter is undefined...
The entire string is placed into the first element ([0]
) of the returned array."
It's nice that the entire string is returned. Your confusion lies in dealing with the returning of an empty string. The array has a length of 1 because something was returned and put there, but if you check you think you find nothing since it's an empty string like ""
...
Test 1: (string with content)
myTextbox.text = "hello world";
var myArray:Array = myTextbox.text.split("|");
trace("myArray length : " + myArray.length);
trace("myArray [0] : " + myArray[0] );
trace("myArray [0] as Int : " + int(myArray[0]) );
Which gives "hello world"
as first element (at [0]
), since no "|"
exists in string as a delimiter...
myArray len : 1
myArray [0] : hello world
myArray [0] as Int : 0
Test 2: (empty string)
myTextbox.text = "";
var myArray:Array = myTextbox.text.split("|");
trace("myArray length : " + myArray.length);
trace("myArray [0] : " + myArray[0] );
trace("myArray [0] as Int : " + int(myArray[0]) );
Which gives empty ""
as first element (at [0]
), since no "|"
exists in string as a delimiter, the entire empty (zero-length) string now sits at [0]
...
myArray length : 1
myArray [0] :
myArray [0] as Int : 0