13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with strings<br />

Finding substrings and patterns in strings<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

Substrings are sequ<strong>en</strong>tial characters within a string. For example, the string "abc" has the following substrings: "",<br />

"a", "ab", "abc", "b", "bc", "c". You can use ActionScript methods to locate substrings of a string.<br />

Patterns are defined in ActionScript by strings or by regular expressions. For example, the following regular expression<br />

defines a specific pattern—the letters A, B, and C followed by a digit character (the forward slashes are regular<br />

expression delimiters):<br />

/ABC\d/<br />

ActionScript includes methods for finding patterns in strings and for replacing found matches with replacem<strong>en</strong>t<br />

substrings. These methods are described in the following sections.<br />

Regular expressions can define intricate patterns. For more information, see “Using regular expressions” on page 76.<br />

Finding a substring by character position<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The substr() and substring() methods are similar. Both return a substring of a string. Both take two parameters.<br />

In both methods, the first parameter is the position of the starting character in the giv<strong>en</strong> string. However, in the<br />

substr() method, the second parameter is the l<strong>en</strong>gth of the substring to return, and in the substring() method, the<br />

second parameter is the position of the character at the <strong>en</strong>d of the substring (which is not included in the returned<br />

string). This example shows the differ<strong>en</strong>ce betwe<strong>en</strong> these two methods:<br />

var str:String = "Hello from Paris, Texas!!!";<br />

trace(str.substr(11,15)); // output: Paris, Texas!!!<br />

trace(str.substring(11,15)); // output: Pari<br />

The slice() method functions similarly to the substring() method. Wh<strong>en</strong> giv<strong>en</strong> two non-negative integers as<br />

parameters, it works exactly the same. However, the slice() method can take negative integers as parameters, in<br />

which case the character position is tak<strong>en</strong> from the <strong>en</strong>d of the string, as shown in the following example:<br />

var str:String = "Hello from Paris, Texas!!!";<br />

trace(str.slice(11,15)); // output: Pari<br />

trace(str.slice(-3,-1)); // output: !!<br />

trace(str.slice(-3,26)); // output: !!!<br />

trace(str.slice(-3,str.l<strong>en</strong>gth)); // output: !!!<br />

trace(str.slice(-8,-3)); // output: Texas<br />

You can combine non-negative and negative integers as the parameters of the slice() method.<br />

Finding the character position of a matching substring<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You can use the indexOf() and lastIndexOf() methods to locate matching substrings within a string, as the<br />

following example shows:<br />

var str:String = "The moon, the stars, the sea, the land";<br />

trace(str.indexOf("the")); // output: 10<br />

Notice that the indexOf() method is case-s<strong>en</strong>sitive.<br />

Last updated 6/6/2012<br />

15

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!