To create and use strings:
1.
Create a new Flash document and save it as strings.fla.
2.
Add the following ActionScript to Frame 1 of the main Timeline:
var firstStr:String = "foo";
var secondStr:String = new String("foo");
trace(firstStr == secondStr); // true
var thirdStr:String;
trace(thirdStr); // undefined
This code defines three String objects, one that uses a string literal, one that uses the
operator, and the other without an initial value. Strings can be compared by using the
equality (==) operator, as shown in the third line of code. When referring to variables, you
specify the data type only when the variable is being defined.
3.
Select Control > Test Movie to test the document.
Always use string literals unless you specifically need to use a String object. For more
information on string literals and the String object, see
page
130.
To use single straight quotation mark (
within a single string literal, use the backslash character (
following two strings are equivalent:
var firstStr:String = "That's \"fine\"";
var secondStr:String = 'That\'s "fine"';
For more information on using the backslash character in strings, see
character" on page
460.
Remember that you cannot use "curly quotation mark" or "special quotation mark" characters
within your ActionScript code; they are different from the straight quotation marks (
(
) that you can use in your code. When you paste text from another source into
"
ActionScript, such as the web or a Word document, be sure to use straight quote delimiters.
You can find a sample source file, strings.fla, in the Samples folder on your hard disk. This file
shows you how to build a simple word processor that compares and retrieves string and
substring selections.
In Windows, browse to boot drive\Program Files\Macromedia\Flash 8\Samples and
Tutorials\Samples\ActionScript\Strings.
On the Macintosh, browse to Macintosh HD/Applications/Macromedia Flash 8/Samples
and Tutorials/Samples/ActionScript/Strings.
Chapter 5, "About literals," on
) and double straight quotation mark (
'
) to escape the character. The
\
About strings and the String class
new
) delimiters
"
"About the escape
) and
'
459
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?