in black and white
Main menu
Share a book About us Home
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics

Flash MX action script bible - Reinhardt R.

Reinhardt R. Flash MX action script bible - Wiley & sons , 2004. - 987 p.
ISBN: 0-7645-4354-7
Download (direct link): macractionscriptbiblefeb2004.pdf
Previous << 1 .. 177 178 179 180 181 182 < 183 > 184 185 186 187 188 189 .. 427 >> Next

". today is " todaysDate;
But unfortunately, this results in an error because of the missing operator just before todaysDate. So the correct line reads as follows:
var sGreeting:String = "hello, " + username + ^
". today is " + todaysDate;
♦ It is also common to accidentally omit quotes when joining multiple string literals, or even sometimes at the beginning or end of a single string literal. For instance:
var sVal:String = "this is string one." + " " + this is string two";
This code clearly does not work. But sometimes it can be hard to see the missing quotes. The correct line reads as follows:
var sVal:String = "this is string one." + " " + ^
"this is string two";
♦ It is a common mistake to use the = operator instead of the += operator when appending strings such as the following:
var sVal:String = "string one."; sVal += "string two."; sVal = "string three.";
Part IV ♦ The Core Classes
This results in a string with the value of string three. The problem is that the last line uses just the assignment operator instead of the += operator. The correct code reads as follows:
var sVal:String = "string one."; sVal += "string two."; sVal += "string three.";
♦ When you are retrieving string values from a database or other server-side datasource, you sometimes find that extra whitespace characters have been added to the beginning and/or end of the string value. Depending on your usage of the string, that extra whitespace might not have much effect. If you are finding that you are having some kind of issue with your code that could potentially be caused by such extra whitespace, you can add a simple debugging test by outputting the string value with a character such as a ' or a | at the beginning and end so you can see if there are any extra nonvisible characters. For example, if you have a variable named sValue, you would use the following:
trace("|" + sValue + "|");
Then, when you test the application you will be able to quickly see if there is extra whitespace. If the value of sValue i s some text and there is no extra whitespace, it will appear as:
|some text|
On the other hand, if there is an extra space at the end of the text, you can see it:
|some text |
Working with Character Codes
When you are working with strings, there are many characters that you can display beyond the standard characters on the keyboard. Doing so requires the use of the character codes. Each character has a numeric value associated with it. For instance, the letter “a” has the character code of 97. There is a separate character code for upper- and lowercase letters. In the discussion of the charCodeAt() and fromCharCode() methods later in this chapter, you learn how to generate a list of the character codes.
Determining the Number of Characters
Every String object has a length property that reports the number of characters in a String object. You’ve already seen this used in several of the previous examples.
var sTitle:String = new String("ActionScript Bible"); trace(sTitle.length); // Displays: 18
All characters in a String object’s value are counted. This includes spaces, punctuation, and special character sequences. Even the backspace sequence counts as a character. This might seem counterintuitive, but it is true nonetheless.
Working with Substring Values
A substring is made up of a portion of another string. For example, “accord” is a substring of “accordion”. A substring can be a single character or the entire original string. The slice(), substring(), and substr() methods of the String class are all used for selecting a
Chapter 15 ♦ Working with Strings 373
substring value. Each works in a slightly different way, but all return a new string value without changing the String object. In addition, the charAt() method returns a single-character substring.
The substr() method allows you to select a substring by specifying a starting point and a length. Each character in a String object’s value is assigned an index. The first character has an index of 0, the second has an index of 1, and so on. Figure 15-1 helps to illustrate this.
0 i 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
A c t i o n S c r i P t B i b l e
Figure 15-1: The characters of a String object are indexed with numbers. The first character has an index of 0.
The following is an example of how to use the substr() method:
var sTitle:String = new String("ActionScript Bible"); trace(sTitle.substr(6, 6)); // Displays: Script
trace(sTitle.substr(0, 6)); // Displays: Action
trace(sTitle.substr(0, 12)); // Displays: ActionScript
In this example, the value assigned to the String object is ActionScript Bible. Then, you can display various substrings such as Script, Action, and ActionScript.
You may optionally omit the second parameter. When you do this, it returns a substring starting at the specified index and going to the end of the original string. For example:
var sTitle:String = new String("ActionScript Bible"); trace(sTitle.substr(6)); // Displays: Script Bible
Previous << 1 .. 177 178 179 180 181 182 < 183 > 184 185 186 187 188 189 .. 427 >> Next