View comments | RSS feed

About using operators with strings

Comparison operators compare strings only if both operands are strings. An exception to this rule is the strict equality (===) operator. If only one operand is a string, ActionScript converts both operands to numbers and performs a numeric comparison on them. For more information on numeric operators, see Using numeric operators.

Except for the equality operator (==), comparison operators (>, >=, <, and <=) affect strings differently than when they operate on other values.

Comparison operators compare strings to determine which is first alphabetically. Strings with uppercase characters precede strings that are lowercase. That means that "Egg" comes before "chicken".

var c:String = "chicken";
var e:String = "Egg";
trace(c < e); // false
var riddleArr:Array = new Array(c, e);
trace(riddleArr); // chicken,Egg
trace(riddleArr.sort()); // Egg,chicken

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 and Using relational and equality operators.

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

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.

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.

The previous example shows how you can use the addition (+) and addition assignment (+=) operators to concatenate strings. Notice how the third line of code uses the addition operator to concatenate the value of the myNum variable into the text field, and the fourth line of code uses the addition assignment operator to concatenate a string onto the existing value of the text field.

If only one of the text string operands is actually a string, Flash converts the other operand into a string. Therefore, the value of myNum converts to a string in the previous example.

NOTE

 

ActionScript treats spaces at the beginning or end of a string as a literal part of the string.


Version 8

Comments


Fumio Nonaka said on Oct 12, 2005 at 9:18 AM :
The strict equality (===) operator returns false if the data type of operands are different.

[Wrong]: Comparison operators compare strings only if both operands are strings. An exception to this rule is the strict equality (===) operator. If only one operand is a string, ActionScript converts both operands to numbers and performs a numeric comparison on them.

[Correct]: Comparison operators converts a operand to the type of the other if the both data types are different. If one operand is a string and the other is a number, ActionScript converts the string operand to a number and performs a numeric comparison on them.
jdehaan said on Nov 1, 2005 at 2:27 PM :
Correct (and thanks!) FYI, we also still need to mention the strict equality in there. A combination of the original and revised would be:

Comparison operators converts a operand to the type of the other if the both data types are different. If one operand is a string and the other is a number, ActionScript converts the string operand to a number and performs a numeric comparison on them. An exception to this rule is the strict equality (===) operator which performs in the same way as the equality (==) operator, except that data types are not converted. The result is true if both expressions, including their data types, are equal.

Thanks!
Jen.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001265.html