Previous Page
Next Page

Recipe 12.1. Joining Strings

Problem

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

Solution

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

Discussion

Multiple strings can be joined in a single expression using the concatenation operator, +, between two string operands:

// This results in a single value of "Thisworks" (no space)
var example:String = "This" + "works";

If you want to join 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)
var example:String = "This" + " " + "works";

In the preceding examples, there is little reason why you would need to join 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 not only string literals, but also variables containing string values (or values that can be converted to strings). For example:

var attendance:int = 24;
// Results in a single value of "There are 24 people"
var output:String = "There are " + attendance + " people";

The concatenation operator automatically converts any nonstring values to strings, as long as at least one of the operands in the statement is a string. In the preceding example, the numerical 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:

var first:int = 24;
var second:int = 42;
// Results in the compiler error, "Implicit coercion of a value 
// type 'Number' to an unrelated type 'String'"
var result:String = first + second;

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:

var first:int = 24;
var second:int = 42;
// Results in a string value of "2442"
var result:String = "" + first + second;

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:

var first:int = 24;
var second:int = 42;
// Results in a string value of "66"
var result:String = first + second + "";

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

var first:int = 24;
var second:int = 42;
// Results in a string value of "2442"
var result:String = String( first ) + second;

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:

var first:int = 24;
var second:int = 42;
var third:int = 21;
// Results in a string value of "6621"
var result:String = first + second + String( third );

Yet another option is to use the toString( ) method available on most built-in objects. Both Number and int data types have a toString( ) method available:

var first:int = 24;
var second:int = 42;
var third:int = 21;
// Results in a string value of "244221"
var result:String = first.toString(  ) + second + third;

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 changes the order of operation and evaluates the inner expression first, treating it as an addition operation rather than a concatenation operation:

var first:int = 24;
var second:int = 42;
// Results in "There are 66 people"
var result:String = "There are " + ( first + second ) + " people";

You can also append text to existing strings by using the concatenation assignment += operator:

var attendance:int = 24;
var example:String = "There are ";
example += attendance;
// Results in a string value of "There are 24 people"
example += " people";

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

var example:String = "This is the first sentence in a long paragraph of text.";
example += "By adding line by line to the string variable you make ";
example += "your code more readable.";

However, instead of using three separate statements the preceding code can be expressed more efficiently by using clever spacing and line breaking with the regular + operator. Note that there is only one ; making the entire assignment statement expand over three lines:

var example:String = "This is the first sentence in a long paragraph of text. "
    + "By splitting the long string into smaller, more manageable pieces "
    + "and using the + operator, you can make your code more readable.";

The most practical use of concatenation assignment is when you need to append more text to a string over time rather than all at once. For example, you may have a chat history window that updates whenever someone contributes text to the conversation. You can append the new text to an existing string by using the combination concatenation assignment operator. For example:

// A method that will append the username and message to 
// a chat history string variable
private function updateChatHistory( message:String, username:String ):void {
  // Assume history is a string variable that contains past conversation history.
  // Append the username and message with a new line to the existing string.
  _history += username + ":" + message + '\n';
};

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 result of the concatenation:

var original:String = "original string value.";

// Set modified to "original string value.now modified." 
// The value of original remains unchanged.
var modified:String = original.concat( "now modified." );

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.


See Also

Recipe 12.6


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)