Recipe 9.1 Joining Strings

9.1.1 Problem

You want to concatenate (join) together two or more strings into a single value.

9.1.2 Solution

Use the string concatenation operator (+) or the combination concatenation-assignment operator (+=). Alternatively, use the concat( ) method.

9.1.3 Discussion

You can join together multiple strings in a single expression using the concatenation operator (+) between the two string operands:

// Results in a single value of "Thisworks" (no space)
myString = "This" + "works";

If you want to join together more than two strings, use additional concatenation operators and string value operands in the appropriate order:

// Results in a single value of "This works" (with a space)
myString = "This" + " " + "works";

In the preceding examples, there is little reason why you would need to join together the string literals instead of assigning a single string value ("This works" instead of "This" + " " + "works"). However, this demonstrates the technique you'll use when working with dynamic values. You can use the concatenation operator to join together not only string literals, but also variables containing string values (or values that can be converted to strings). For example:

num = 24.
// Results in a single value of "There are 24 people"
myString = "There are " + num  + " people";

The concatenation operator automatically converts any non-string values to strings, as long as at least one of the operands in the statement is a string. In the preceding example, the number value, 24, is converted to the string value "24" automatically before being joined with the other strings. However, if all the operands are numbers, the ActionScript interpreter treats the + operator as the addition operator instead of the concatenation operator:

num1 = 24;
num2 = 42;
// Results in a number value of 66
myString = num1 + num2;

You can concatenate, rather than add, two or more numbers in several ways. One way is to concatenate an empty string to the beginning of the statement:

num1 = 24;
num2 = 42;
// Results in a string value of "2442"
myString = "" + num1 + num2;

The empty string must be placed first in the expression because if the two numbers appear first, they are added rather than concatenated, even though the final value is still converted to a string:

num1 = 24;
num2 = 42;
// Results in a string value of "66"
myString = num1 + num2 + "";

Another option is to use the String( ) function to ensure that at least one of the numbers is converted to a string before performing the concatenation:

num1 = 24;
num2 = 42;
// Results in a string value of "2442"
myString = String(num1) + num2;

When you use this technique for only two numbers, it does not matter which one you convert to a string (or you can convert both). But if you are joining more than two numbers, you should convert the first or second number to a string. Otherwise, the numbers preceding the value that is converted to a string will be added rather than concatenated.

num1 = 24;
num2 = 42;
num3 = 21;
// Results in a string value of "6621"
myString = num1 + num2 + String(num3);

If you want to add, rather than concatenate, two number values in the middle of a string concatenation statement, you should enclose that expression in parentheses. This evaluates the expression first, treating it as an addition operation rather than a concatenation operation.

num1 = 24;
num2 = 42;
// Results in "There are 66 people"
myString = "There are " + (num1 + num2) + " people";

You can also append text to strings using multiple lines of code rather than trying to cram it all into one statement. You should use the combination concatenation-assignment operator in these cases.

myString = "a ";
myString += "quick ";
myString += "brown ";
myString += "fox";

This technique can be useful for several reasons. First of all, sometimes you want to join together long string values, and your code remains more readable when you break it up into multiple lines:

myString = "This is the first line of a long paragraph of text.\n";
myString += "By adding line by line to the string variable you make ";
myString += "your code more readable.";

You may have a good reason to append more text to a string over time rather than all at once. For example, your movie may periodically poll a server-side script that returns updated data. You can append this new data to an existing string using the combination concatenation-assignment operator. For example:

// lv is a LoadVars object.
lv.onLoad = function (  ) {
  // Assume the server script returns a variable named newData. You can append its
  // value (and a newline) to an existing string named myString.
  _root.myString += this.newData + newline;

You can also use the String.concat( ) method to append new values to the end of an existing string. The concat( ) method does not affect the original string. Instead, it returns a new string containing the concatenated value.

myString = "first string value.";

// Set myNewString to "first string value.second string value." 
// The value of myString remains unchanged.
myNewString = myString.concat("second string value.");

Flash 4 used the & operator for string concatenation. Flash converts the & operator to the add operator when updating Flash 4 files to Flash 5 or Flash 6 format. The + operator is the preferred string concatenation operator in Flash 5 and later.

    Part I: Local Recipes
    Part II: Remote Recipes