User Guide
209
7
CHAPTER 7
Working with Strings
The String class contains methods that let you work with text strings. Strings are important in
working with many objects. The methods described in this chapter are useful in working with
strings used in objects such as TextField, StaticText, XML, ContextMenu, and FileReference
objects.
Strings are sequences of characters. ActionScript 3.0 supports ASCII and Unicode characters.
Contents
Creating strings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
The length property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Working with characters in strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211
Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Obtaining string representations of other objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Concatenating strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Finding substrings and patterns in strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Converting strings between uppercase and lowercase . . . . . . . . . . . . . . . . . . . . . . . 219
Example: ASCII Art . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Creating strings
The String class is used to represent string (textual) data in ActionScript 3.0. ActionScript
strings support both ASCII and Unicode characters. The simplest way to create a string is to
use a string literal. To declare a string literal, use straight double quotation mark (
") or single
quotation mark (
') characters. For example, the following two strings are equivalent:
var str1:String = "hello";
var str2:String = 'hello';
You can also declare a string by using the new operator, as follows:
var str1:String = new String("hello");