466 Working with Text and Strings
To create an array of substrings segmented by delimiter:
1. Create a new Flash document and save it as strsplit.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var queryStr:String = "first=joe&last=cheng&title=manager&startDate=3/6/
65";
var params:Array = queryStr.split("&", 2);
trace(params); // first=joe,last=cheng
/* params is set to an array with two elements:
params[0] == "first=joe"
params[1] == "last=cheng"
*/
3.
Select Control > Test Movie to test the Flash document.
var queryStr:String = "first=joe&last=cheng&title=manager&startDate=3/6/
65";
var my_lv:LoadVars = new LoadVars();
my_lv.decode(queryStr);
trace(my_lv.first); // joe
For more information on using operators with strings, see “About using operators with
strings” on page 182.
You can find a sample source file, strings.fla, in the Samples folder on your hard disk. This file
shows you how to build a simple word processor that compares and retrieves string and
substring selections.
■ In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Strings.
■ On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Strings.
TIP
The second parameter of the split() method defines the maximum size of the array.
If you don’t want to limit the size of the array created by the
split() method, you can
omit the second parameter.
TIP
The easiest way to parse a query string (a string delimited with & and = characters) is
to use the
LoadVars.decode() method, as shown in the following ActionScript: