
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Working with Strings
|
97
The only difference is that since lastIndexOf() searches a string backward, start_index
refers to the rightmost character we want included in our search (not the leftmost). If
start_index is omitted, it defaults to string.length – 1 (the last character in the
string).
For example:
paradox = "pain is pleasure, pleasure is pain";
paradox.lastIndexOf("pain"); // Returns 30; indexOf( ) would return 0
The following returns 0 (the index of the first occurrence of the word “pain”),
because we started the backward search before the second occurrence of “pain”:
paradox.lastIndexOf("pain",29); // Returns 0
No regular expressions
Note that regular expressions (a powerful tool used to recognize patterns in textual
data) are not supported in ActionScript by default. However, developer Pavils Jur-
jans has made a custom RegExp object available at http://www.jurjans.lv/flash/
RegExp.html.
Retrieving Portions of Strings
Sometimes a long string contains a sequence of characters that we’d like to access
more conveniently. In the string “Steven Sid Mumby”, for example, we may want to
extract the last name, “Mumby”. To extract a shorter string (or substring), we use
one of these functions: substring(), substr(), splice(),orsplit(). We’ll cover how each
of them work before discussing performance ...