O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6.7. Converting an Array to a String

Problem

You want to convert an array to a string.

Solution

Use the join( ) method.

Discussion

ActionScript provides you with a built-in way to quickly convert arrays to strings (assuming, of course, that the array elements themselves are either strings or another datatype that ActionScript can automatically cast to a string) using the join( ) method. You should provide the join( ) method with a parameter that tells Flash which delimiter to use to join the elements:

myArray = ["a", "b", "c"];
trace(myArray.join("|"));   // Displays: a|b|c

If you don’t provide a delimiter, Flash uses a comma by default:

myArray = ["a", "b", "c"];
trace(myArray.join(  ));     // Displays: a,b,c

Note

The toString( ) method does the same thing as calling the join( ) method either with no parameters or with the comma as the parameter. And, in fact, if you try to use an array in a situation in which a string is required, Flash will automatically call the toString( ) method:

myArray = ["a", "b", "c"];
trace(myArray);  // Displays: a,b,c

See Also

Recipe 6.6

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required