User Guide

Concatenating strings 213
Obtaining string representations of other
objects
You can obtain a String representation for any kind of object. All objects have a toString()
method for this purpose:
var n:Number = 99.47;
var str:String = n.toString();
// str == "99.47"
When using the + concatenation operator with a combination of String objects and objects
that are not strings, you do not need to use the
toString() method. For details on
concatenation, see the next section.
The
String() global function returns the same value for a given object as the value returned
by the object calling the
toString() method.
Concatenating strings
Concatenation of strings means taking two strings and joining them sequentially into one.
For example, you can use the
+ operator to concatenate two strings:
var str1:String = "green";
var str2:String = "ish";
var str3:String = str1 + str2;
// str3 == "greenish"
You can also use the += operator to the produce the same result, as the following example
shows:
var str:String = "green";
str += "ish";
// str == "greenish"
Additionally, the String class includes a concat() method, as follows:
var str1:String = "Bonjour";
var str2:String = "from";
var str3:String = "Paris";
var str4:String = concat(str1, " ", str2, " ", str3)
// str4 == "Bonjour from Paris"
If you use the + operator (or the += operator) with a String object and an object that is not a
string, ActionScript automatically converts the nonstring object to a String object in order to
evaluate the expression, as shown in this example:
var str:String = "Area = ";
var area:Number = Math.PI * Math.pow(3, 2);