About strings and the String class 465
The preceding code shows two methods of concatenating strings. The first method uses
the addition (+) operator to join the
str1 string with the string "ish". The second
method uses the addition and assignment (+=) operator to concatenate the string
"ish"
with the current value of
str3.
3. Save the Flash document and select Control > Test Movie.
You can also use the
concat() method of the String class to concatenate strings. This method
is demonstrated in the following example.
To concatenate two strings with the concat() method:
1. Create a new Flash document and save it as concat2.fla.
2. Add the following code to Frame 1 of the Timeline:
var str1:String = "Bonjour";
var str2:String = "from";
var str3:String = "Paris";
var str4:String = str1.concat(" ", str2, " ", str3);
trace(str4); // Bonjour from Paris
3.
Select Control > Test Movie to test the Flash document.
If you use the addition (
+) operator (or the addition and assignment [+=] operator) with a
string and a nonstring object, ActionScript automatically converts nonstring objects to strings
in order to evaluate the expression. This conversion is demonstrated in the following
code example:
var version:String = "Flash Player ";
var rel:Number = 8;
version = version + rel;
trace(version); // Flash Player 8
However, you can use parentheses to force the addition (+) operator to evaluate arithmetically,
as demonstrated in the following ActionScript code:
trace("Total: $" + 4.55 + 1.46); // Total: $4.551.46
trace("Total: $" + (4.55 + 1.46)); // Total: $6.01
You can use the split() method to create an array of substrings of a string, which is divided
based on a delimiter character. For example, you could segment a comma- or tab-delimited
string into multiple strings.
For example, the following code shows how you can split an array into substrings by using the
ampersand (
&) character as a delimiter.