EasyManua.ls Logo

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

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...
About operators 183
In this ActionScript, the sort() method of the Array class reorders the contents of the array
alphabetically. You can see that the value “Egg” comes before the value chicken” because
uppercase E comes before a lowercase c. If you want to compare the strings regardless of case,
you need to convert the strings to uppercase or lowercase before you compare them. For more
information on comparison operators, see About equality operators” on page 189 and “Using
relational and equality operators” on page 190.
You can use the
toLowerCase() or toUpperCase() methods to convert strings to a similar
case before you compare them. In the following example, both strings convert to lowercase
strings and compare, and now the chicken comes before the egg:
var c:String = "chicken";
var e:String = "Egg";
trace(c.toLowerCase() < e.toLowerCase()); // true
You can use operators to manipulate strings. You can use the addition (+) operator to
concatenate string operands. You might have already used the addition operator to
concatenate strings when you write
trace statements. For example, you might write the
following:
var myNum:Number = 10;
trace("The variable is " + myNum + ".");
When you test this code, the Output panel displays the following:
The variable is 10.
In the previous example, the trace statement uses the + operator to concatenate instead of
add. When you deal with strings and numbers, Flash sometimes concatenates instead of
adding numerically.
For example, you might concatenate two strings from different variables in a single text field.
In the following ActionScript code, the variable
myNum concatenates with a string, and the
string is displayed in the
myTxt text field on the Stage.
this.createTextField("myTxt", 11, 0, 0, 100, 20);
myTxt.autoSize = "left";
var myNum:Number = 10;
myTxt.text = "One carrot. " + myNum + " large eggplants.";
myTxt.text += " Lots of vegetable broth.";
This code outputs the following in a text field with the instance name myTxt:
One carrot. 10 large eggplants. Lots of vegetable broth.
NOTE
Comparison operators compare only two strings. For example, the operators do not
compare the values if one operand is a numerical value. If one of the operands is a string,
ActionScript converts both operands to numbers and then compares them numerically.

Table of Contents

Related product manuals