Chapter 12. Strings

Introduction

Strings are the fundamental textual element of the ActionScript language. A string is a series of zero or more characters enclosed in single or double quotes. Unlike some other languages, ActionScript does not differentiate between single characters and strings. Both characters and strings are grouped into the String datatype. For example:

var exampleA:String = "this is a string";
var exampleB:String = 'this is also a string';
var exampleC:String = "strings can contain characters such as –(*+5~";
var exampleD:String = ""; // Empty string
var exampleE:String = "x"; // Single character
var exampleF:String; // Defaults to null when no value is assigned

String values must always be enclosed within quotes. You can use either single or double quotes, but the starting and ending quotes enclosing a string must be of the same type.

// Both of these strings cause errors because of mismatched quotes.
var exampleA:String = "an incorrect string';      // Ending quote should be double
var exampleB:String = 'another incorrect string"; // Ending quote should be single

ActionScript provides functionality that allows you to work with strings and characters in many ways. A new feature of ActionScript 3.0 relating to strings is the built-in support for regular expressions (pattern matching), which is covered in Chapter 13.

Joining Strings

Problem

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

Solution

Use the string concatenation operator +, the ...

Get ActionScript 3.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.