EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 468

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
468 Working with Text and Strings
To find the character position of a matching substring:
1. Create a new Flash document and save it as indexof.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var myStr:String = "The moon, the stars, the sea, the land";
trace(myStr.indexOf("the")); // 10
trace(myStr.indexOf("the", 11)); // 21
The first index of the word the begins at the 10th character because the indexOf()
method is case sensitive; therefore the first instance of The isnt considered. You can also
specify a second parameter to the
indexOf() method to indicate the index position in the
string from which to start the search. In the preceding code, Flash searches for the first
index of the word the that occurs after the 11th character.
3. Add the following ActionScript below the code that you added in the previous step:
trace(myStr.lastIndexOf("the")); // 30
trace(myStr.lastIndexOf("the", 29)); // 21
The lastIndexOf() method finds the last occurrence of a substring in the string. For
example, instead searching for a character or substring from the beginning of a string,
lastIndexOf() starts from the end of a string and works backward. Similarly to the
indexOf() method, if you include a second parameter with the lastIndexOf() method,
the search is conducted from that index position, although with
lastIndexOf() the
string is searched backward (from right to left).
4. Select Control > Test Movie to test your Flash document.
You can find a sample source file, strings.fla, in the Samples folder on your hard disk. This file
shows you how to build a simple word processor that compares and retrieves string and
substring selections.
In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Strings.
On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Strings.
TIP
The indexOf() and lastIndexOf() methods are case sensitive.

Table of Contents

Related product manuals