User Guide

216 Working with Strings
Creating an array of substrings segmented by a
delimiter
You can use the split() method to create an array of substrings, which is divided based on a
delimiter. For example, you can segment a comma-delimited or tab-delimited string into
multiple strings.
The following example shows how to split an array into substrings with the ampersand (&)
character as the delimiter:
var queryStr:String = "first=joe&last=cheng&title=manager&StartDate=3/6/
65";
var params:Array = queryStr.split("&", 2);
// params == ["first=joe","last=cheng"]
The second parameter of the split() method, which is optional, defines the maximum size
of the array that is returned.
You can also use a regular expression as the delimiter character:
var str:String = "Give me\t5."
var a:Array = str.split(/\s+/);
// a == ["Give","me","5."]
For more information, see Chapter 10, “Using Regular Expressions,” on page 285 and the
ActionScript 3.0 Language Reference.
Finding patterns in strings and replacing substrings
The String class includes the following methods for working with patterns in strings:
Use the match() and search() methods to locate substrings that match a pattern.
Use the replace() method to find substrings that match a pattern and replace them with
a specified substring.
These methods are described in the following sections.
You can use strings or regular expressions to define patterns used in these methods. For more
information on regular expressions, see Chapter 10, “Using Regular Expressions,” on
page 285.