Actionscript-the-Definitive

Working with Strings

By manipulating strings we can program anything from a user-input validator to a word-scramble game. With a little ingenuity, we can make neat visual text effects and other fun stuff.

We can manipulate strings with both operators and built-in functions. String operators can join multiple strings together or compare the characters of two strings. Built-in functions can examine a string’s properties and contents, extract a portion of a string, check a character’s code point, create a character from a code point, change the case of the characters in a string, and even turn a string into a variable or property name.

Joining Strings Together

Joining strings together (creating a new string from two or more strings) is called concatenation. As seen earlier, we can concatenate two strings with the plus operator (+), like this:

"Macromedia" + "Flash"

That line of code yields the single string value “MacromediaFlash”. Oops! We forgot to put a space between the words. To add the space, we can insert it within the quotes that define one of the strings, such as:

"Macromedia " + "Flash"  // Yields "Macromedia Flash"

But that’s not always practical. In most cases we don’t want to add a space to a company or a product name. So instead, we join three strings together, the middle one of which is simply an empty space:

"Macromedia" + " " + "Flash"   // Also yields "Macromedia Flash"

Note that the space character is not the same as the empty string we saw earlier because the empty string has no characters between the quotes.

We can also concatenate variables that contain string data. Consider the following code:

var company = "Macromedia";
var product = "Flash";

// Set the variable sectionTitle
                   to "Macromedia Flash"
var sectionTitle = company + " " + product;

In lines 1 and 2, we store string values in variables. Then, we join those values together with a space. Two of our string values are contained in variables, one (the space) is a string literal. Not a problem. Happens all the time.

Occasionally, we’ll want to append characters onto an existing string. For example, we could change the tone of a welcome message like this:

var greeting = "Hello";     // Our welcome message
greeting = greeting + "?";  // Our quizzical welcome message: "Hello?"

The preceding code gets the job done, but notice that we have to refer to greeting twice in line 2. To be more efficient, we can use the += operator, which appends the string on its right to the string variable on the left:

var greeting = "Hello";    // Our welcome message
greeting += "?";           // Our 

The concat( ) function

The concat( ) function appends characters to a string, like +=. Because concat( ) is a function, it uses the dot operator, like this:

var product = "Macromedia".concat(" Flash");

var sentence = "How are you";
var question = sentence.concat("?")

Take heed though—unlike +=, the concat( ) function does not alter the string that it is applied to; it merely returns the concatenated string value. In order to make use of that value, we must assign it to a variable or other data container. Study the following code closely so you’ll understand the difference between += and concat( ) :

var greeting = "Hello";
greeting.concat("?");
trace(greeting);  // Displays "Hello"; greeting was unaffected by concat

finalGreeting = greeting.concat("?");
trace(finalGreeting);  // Displays "Hello?"

The concat( ) function also accepts multiple arguments (that is, it can combine multiple comma-separated strings into one string):

firstName = "Karsten";

// Sets finalGreeting to "Hello Karsten?"
finalGreeting = greeting.concat(" ", firstName, "?");

which is the same as:

finalGreeting = greeting;
finalGreeting += " " + firstName 


Comments