Chapter 6. Text, Strings, and Characters

FEATURED CLASS

  • String

Text in ActionScript is represented as a string, as in "the book consisted entirely of a long string of incomprehensible characters." A string can be any piece of textual data from a single letter to a word to the entirety of Wikipedia. Strings don't have to contain legible prose: they might also contain or be exclusively made of numbers or symbols. Strings can also handle multiple languages. Strings are an essential building block of virtually all programs, so ActionScript 3.0 provides several methods for working with, manipulating, searching, and converting strings.

Working with String Literals

Of course, because they're so indispensable, you've already been exposed to strings before this chapter. Most of what you've seen so far has been string literals — pieces of text surrounded by quotation marks added directly into the code. But as you learned in Chapter 2, "ActionScript 3.0 Language Basics," everything in ActionScript 3.0 is an object, including strings. When you type a string like "Hello, world!" you're actually creating an instance of the String class. The String class offers a load of extra functionality beyond the capability to store text.

Tip

Although technically there is a difference between a string created with a new String() statement and a string created using a string literal, the compiler converts string literals into string objects automatically. Therefore, the following code is perfectly valid:

"banana".length; ...

Get ActionScript 3.0 Bible 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.