About strings and the String class
461
If a string doesn’t contain a value, its length is set to undefined:
var thirdStr:String;
trace(thirdStr.length); // undefined
You can also use character codes to define a string. For more information on character codes
and character encoding, see
“About strings and the String class” on page 450
.
The following example creates a variable named
myStr
and sets the string’s value based on
ASCII values passed to the
String.fromCharCode()
method:
var myStr:String =
String.fromCharCode(104,101,108,108,111,32,119,111,114,108,100,33);
trace(myStr); // hello world!
Each number listed in the
fromCharCode()
method in the previous code represents a single
character. For example, the ASCII value 104 represents a lowercase
h
, and the ASCII value 32
represents the space character.
You can also use the
String.fromCharCode()
method to convert Unicode values, although
the unicode value must be converted from hexidecimal to decimal values, as shown in the
following ActionScript:
// Unicode 0068 == "h"
var letter:Number = Number(new Number(0x0068).toString(10));
trace(String.fromCharCode(letter)); // h
You can examine the characters in various positions in a string, as in the following example.
To loop over a string:
1.
Create a new Flash document.
2.
Add the following ActionScript to Frame 1 of the main Timeline:
var myStr:String = "hello world!";
for (var i:Number = 0; i < myStr.length; i++) {
trace(myStr.charAt(i));
}
3.
Select Control > Test Movie to preview your Flash document. You should see each
character traced to the Output panel on a separate line.
WARN
ING
If your string contains a null byte character (\0), the string value will be truncated.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...