
< Day Day Up >
Working with String and Selection Classes
As one of the most commonly used classes, the String class uses methods that can be
helpful for modifying and building strings—quote-enclosed values that contain
information (like the name "Jobe") that can be easily understood by humans.
var message:String = "No shoes, no service!";
The script creates a variable named message whose value is a string. In such cases, you
can think of the variable itself as a String object (an instance of the String class)—which
means you can use String class methods to manipulate its value.
Let's look at a few examples.
The toUpperCase() method of the String class forces a string to become all uppercase
letters:
message = message.toUpperCase();
The script modifies the variable message to contain "NO SHOES, NO SERVICE!" For
the opposite effect—that is, to force the entire string to become all lowercase letters—you
would apply toLowerCase() in the same fashion.
N
ote that the text value of a text field instance (the text within the field) is considered an
instance of the String object. If message were the name of a text field instance, the
previous script could be rewritten like this:
message.text = message.text.toUpperCase();
Although it is most common to create a String object the easy way (var message:String =
"Hello"), it is also possible to create a String object by using the constructor of the String
class.

var message:String = new String("Hello");
Is there any advantage to using one over the other? Not really: every project is different,
and you may find cases where one seems to be more appropriate than the other. To
review, you can create an instance of the String object by:
• Using a constructor (for example, var myNewStringObject:String = new
String("Hello");). The String object is identified as myNewStringObject.
• Assigning a string value to a variable. The String object is identified as the name
of the variable.
• Creating a text field. The String object is identified as the text property of the
field, as in nameOfTextField.text.
Another useful method of the String class—indexOf()—lets you find the first occurrence
of a character or group of characters in a string. The result returned is a number
corresponding to the letter index where the string starts. A letter index is the number of a
character in relation to the whole string. The first character in a string has an index of 0,
the second has an index of 1, and so on. If the indexOf() method finds no occurrences of
the character or group of characters, it returns a value of -1. Here's an example of one use
of the indexOf() method:
message_txt.text = "No shoes, no service!";
var firstS:Number = message_txt.text.indexOf("s");
The variable firstS will be assigned a value of 3 because that's the character number of
the first s encountered (the first s in shoes).
It can sometimes be useful to determine the number of characters in a string. This is easy
to do because all instances of the string class have a length property. You can often use
string length to validate user-input information in a text field. Perhaps you want a user to

enter a valid zip code: you know that it must be five characters in length. By checking the
length property of the zip code, you could create a simple validation script:
zipCode_txt.text = "27609";
var zipLength:Number = zipCode_txt.text.length
if (zipLength == 5) {
// Correct length of zip code
} else {
// Invalid zip code
}
The first line sets the text value shown in the zipCode_txt text field. (We assume this is
what the user has entered.) The next line creates a variable named zipLength and assigns
it a value based on the length property of zipCode_txt.text—in this case 5, because that's
the number of characters the zipCode_txt field contains. The last part of the script uses an
if statement to take one set of actions if zipLength equals 5 and another if it doesn't.
The Selection class allows you to control various aspects of the currently focused text
field, including highlighting text, getting and setting the caret's (current insertion point)
position and more. A text field is considered focused if the user's cursor is placed there.
Because only one text field can be focused at a time, there's no need to create an instance
of the Selection class; you can use it directly (the Selection class is a top-level class). By
clicking on a text field, the user dictates which one has focus. However, you can use the
setFocus() method to override the user's current choice—important since you can only
use other Selection class methods on the text field currently in focus. (As you'll see in the
next exercise, you can't always rely on the user to select—and thus bring into focus—the
proper text field.)
One last method of the Selection class allows you to highlight portions of text
dynamically—without the user's help. The method Selection.setSelection(param1,
param2) includes two parameters: the character index of where the selection starts and
the character index of where the selection should end. For example, you have a text field
that contains the text "Derek is the craziest person I know". To highlight the word
craziest (assuming its text field is in focus), you would use:

Selection.setSelection(13, 20);
In this exercise, you'll create a simple word processor using many of the String and
Selection class methods we've discussed.
1. Open wordProcessor1.fla in the Lesson04/Assets folder.
This file has three layers: Background, Buttons, and Actions. The Background
layer contains the image of the word processor window as well as text fields. The
Buttons layer contains the four buttons that appear at the top of the word processor
window. Starting from the left, the buttons have these instance names: upper_btn,
lower_btn, count_btn, and find_btn.
There are three text field instances on the stage. The largest one, in the center,
represents the text document. It has an instance name of inputField_txt. The next
text field, to the right of the Find button, is appropriately called findField_txt.
When the movie is played, the user will be able to enter a character or string of
characters into this text field, which will be searched against the contents in the
inputField_txt text field. The search results will be displayed in the third text field,
at the bottom of the window, which is called status_txt. This text field is also used
to display the results when counting the number of characters in the document.

2. With the Actions panel open, select Frame 1 in the Actions layer and add the
script:
3.
4. upper_btn.onRelease = function() {
5.
6. inputField_txt.text = inputField_txt.text.toUpperCase();
7.
8. };
9.
Flash interprets this script as saying, "On release of the button called upper_btn,
make all of letters in the inputField_txt text field uppercase." When the button is
clicked, the contents of the inputField_txt text field are reassigned with uppercase
letters.
3. Add this script to the same frame:
4.
5. lower_btn.onRelease = function() {
6.
7. inputField_txt.text = inputField_txt.text.toLowerCase();
8.
9. };
10.