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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Working with strings<br />

Obtaining string repres<strong>en</strong>tations of other objects<br />

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

You can obtain a String repres<strong>en</strong>tation for any kind of object. All objects have a toString() method for this purpose:<br />

var n:Number = 99.47;<br />

var str:String = n.toString();<br />

// str == "99.47"<br />

Wh<strong>en</strong> using the + concat<strong>en</strong>ation operator with a combination of String objects and objects that are not strings, you do<br />

not need to use the toString() method. For details on concat<strong>en</strong>ation, see the next section.<br />

The String() global function returns the same value for a giv<strong>en</strong> object as the value returned by the object calling the<br />

toString() method.<br />

Concat<strong>en</strong>ating strings<br />

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

Concat<strong>en</strong>ation of strings means taking two strings and joining them sequ<strong>en</strong>tially into one. For example, you can use<br />

the + operator to concat<strong>en</strong>ate two strings:<br />

var str1:String = "gre<strong>en</strong>";<br />

var str2:String = "ish";<br />

var str3:String = str1 + str2; // str3 == "gre<strong>en</strong>ish"<br />

You can also use the += operator to the produce the same result, as the following example shows:<br />

var str:String = "gre<strong>en</strong>";<br />

str += "ish"; // str == "gre<strong>en</strong>ish"<br />

Additionally, the String class includes a concat() method, which can be used as follows:<br />

var str1:String = "Bonjour";<br />

var str2:String = "from";<br />

var str3:String = "Paris";<br />

var str4:String = str1.concat(" ", str2, " ", str3);<br />

// str4 == "Bonjour from Paris"<br />

If you use the + operator (or the += operator) with a String object and an object that is not a string, ActionScript<br />

automatically converts the nonstring object to a String object in order to evaluate the expression, as shown in this<br />

example:<br />

var str:String = "Area = ";<br />

var area:Number = Math.PI * Math.pow(3, 2);<br />

str = str + area; // str == "Area = 28.274333882308138"<br />

However, you can use par<strong>en</strong>theses for grouping to provide context for the + operator, as the following example shows:<br />

trace("Total: $" + 4.55 + 1.45); // output: Total: $4.551.45<br />

trace("Total: $" + (4.55 + 1.45)); // output: Total: $6<br />

Last updated 6/6/2012<br />

14

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

Saved successfully!

Ooh no, something went wrong!