Recipe 9.3 Inserting Special Whitespace Characters

9.3.1 Problem

You want to add whitespace characters such as tabs or newline characters to your string.

9.3.2 Solution

Use the escape sequences for the special characters.

9.3.3 Discussion

There are five special whitespace characters with escape sequences, as shown in Table 9-1.

Table 9-1. Whitespace escape sequences

Whitespace character

Escape sequence

Newline

\n

Tab

\t

Backspace

\b

Form feed

\f

Carriage return

\r

You can use these escape sequences within a string. They are most useful when displaying a string value in a text field:

// Results in a string value: these    words    are    separated    by    tabs myString = "these\twords\tare\tseparated\tby\ttabs"; /* Results in a string value: these words are separated by newlines */ myString = "these\nwords\nare\nseparated\nby\nnewlines";

ActionScript also provides a constant, newline, which can be used in place of the \n escape sequence. The result is the same:

/* Results in a string value: two lines */ myString = "two" + newline + "lines";

Within Flash, the newline, form feed, and carriage return characters all result in the same display. However, when you load content into Flash from external sources, some values will have newline characters, some will have form feeds, and some will have carriage returns.



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net