EasyManua.ls Logo

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

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...
462 Working with Text and Strings
4. Modify the existing ActionScript code so that it traces the ASCII value for each character:
var myStr:String = "hello world!";
for (var i:Number = 0; i < myStr.length; i++) {
trace(myStr.charAt(i) + " - ASCII=" + myStr.charCodeAt(i));
}
5.
Save the current Flash document and select Control > Test Movie to preview the SWF file.
When you run this code, the following is displayed in the Output panel:
h - ASCII=104
e - ASCII=101
l - ASCII=108
l - ASCII=108
o - ASCII=111
- ASCII=32
w - ASCII=119
o - ASCII=111
r - ASCII=114
l - ASCII=108
d - ASCII=100
! - ASCII=33
You can use operators to compare strings. For information on using operators with strings, see
About using operators with strings” on page 182.
You can use these operators with conditional statements, such as
if and while. The following
example uses operators and strings to make a comparison.
To compare two strings:
1. Create a new Flash document and save it as comparestr.fla.
2. Add the following ActionScript to Frame 1 of the main Timeline:
var str1:String = "Apple";
var str2:String = "apple";
if (str1 < str2) {
trace("Uppercase letters sort first.");
}
3.
Save the Flash document and select Control > Test Movie to test the SWF file.
You can use the equality (==) and inequality (!=) operators to compare strings with other
types of objects, as shown in the following example.
TIP
You can also split a string into an array of characters by using the String.split()
method and entering an
empty string ("") as a delimiter; for example, var
charArray:Array = myStr.split("");

Table of Contents

Related product manuals