About strings and the String class 463
To compare strings to other data types:
1. Create a new Flash document and save it as comparenum.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var myStr:String = "4";
var total:Number = 4;
if (myStr == total) {
trace("Types are converted.");
}
3.
Save the Flash document and select Control > Test Movie to test the SWF file.
When comparing two different data types (such as strings and numbers), Flash tries to
convert the data types so that a comparison can be made.
Use the strict equality (
===) and strict inequality (!==) operators to verify that both
comparison objects are of the same type. The following example uses strict comparison
operators to ensure that Flash doesn’t try to convert data types while trying to compare values.
To force strict data type comparisons:
1. Create a new Flash document and save it as comparestrict.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var str1:String = "4";
var str2:String = "5";
var total:Number = 4;
if (str1 !== total) {
trace("Types are not converted.");
}
if (str1 !== str2) {
trace("Same type, but the strings don't match.");
}
3.
Save the Flash document and select Control > Test Movie.
For more information on using operators with strings, see “About using operators with
strings” on page 182.
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.