464
Working with Text and Strings
Converting and concatenating strings
You can use the
toString()
method to convert many objects to strings. Most built-in objects
have a
toString()
method for this purpose:
var n:Number = 0.470;
trace(typeof(n.toString())); // string
When you use the addition (
+
) operator with a combination of string and nonstring instances,
you don’t need to use the
toString()
method. For details on concatenation, see the second
procedure in this section.
The
toLowerCase()
method and the
toUpperCase()
method convert alphabetical characters
in the string to lowercase and uppercase, respectively. The following example demonstrates
how to convert a string from lowercase to uppercase characters.
To convert a string from lowercase to uppercase:
1.
Create a new Flash document and save it as
convert.fla
.
2.
Type the following code on Frame 1 of the Timeline:
var myStr:String = "Dr. Bob Roberts, #9.";
trace(myStr.toLowerCase()); // dr. bob roberts, #9.
trace(myStr.toUpperCase()); // DR. BOB ROBERTS, #9.
trace(myStr); // Dr. Bob Roberts, #9.
3.
Save the Flash document and select Control > Test Movie.
myStr = myStr.toUpperCase();
When you concatenate strings, you take two strings and join them sequentially into one
string. For example, you can use the addition (
+
) operator to concatenate two strings. The
next example shows you how to concatenate two strings.
To concatenate two strings:
1.
Create a new Flash document and save it as
concat.fla
.
2.
Add the following code to Frame 1 of the Timeline:
var str1:String = "green";
var str2:String = str1 + "ish";
trace(str2); // greenish
//
var str3:String = "yellow";
str3 += "ish";
trace(str3); // yellowish
NO
TE
After these methods are executed, the source string remains unchanged. To
transform the source string, use the following:
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...