13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with strings<br />

/**<br />

* Capitalizes the first letter of a single word, unless it's one of<br />

* a set of words that are normally not capitalized in English.<br />

*/<br />

private function capitalizeFirstLetter(word:String):String<br />

{<br />

switch (word)<br />

{<br />

case "and":<br />

case "the":<br />

case "in":<br />

case "an":<br />

case "or":<br />

case "at":<br />

case "of":<br />

case "a":<br />

// Don't do anything to these words.<br />

break;<br />

default:<br />

// For any other word, capitalize the first character.<br />

var firstLetter:String = word.substr(0, 1);<br />

firstLetter = firstLetter.toUpperCase();<br />

var otherLetters:String = word.substring(1);<br />

word = firstLetter + otherLetters;<br />

}<br />

return word;<br />

}<br />

In English, the initial character of each word in a title is not capitalized if it is one of the following words: “and,” “the,”<br />

“in,” “an,” “or,” “at,” “of,” or “a.” (This is a simplified version of the rules.) To execute this logic, the code first uses a<br />

switch statem<strong>en</strong>t to check if the word is one of the words that should not be capitalized. If so, the code simply jumps<br />

out of the switch statem<strong>en</strong>t. On the other hand, if the word should be capitalized, that is done in several steps, as<br />

follows:<br />

1 The first letter of the word is extracted using substr(0, 1), which extracts a substring starting with the character<br />

at index 0 (the first letter in the string, as indicated by the first parameter 0). The substring will be one character in<br />

l<strong>en</strong>gth (indicated by the second parameter 1).<br />

2 That character is capitalized using the toUpperCase() method.<br />

3 The remaining characters of the original word are extracted using substring(1), which extracts a substring<br />

starting at index 1 (the second letter) through the <strong>en</strong>d of the string (indicated by leaving off the second parameter<br />

of the substring() method).<br />

4 The final word is created by combining the newly capitalized first letter with the remaining letters using string<br />

concat<strong>en</strong>ation: firstLetter + otherLetters.<br />

G<strong>en</strong>erating the ASCII art text<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

The BitmapToAsciiConverter class provides the functionality of converting a bitmap image to its ASCII text<br />

repres<strong>en</strong>tation. This process is performed by the parseBitmapData() method, which is partially shown here:<br />

Last updated 6/6/2012<br />

23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!