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 />

Using String methods to normalize image titles<br />

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

One of the design decisions for this application is that all the image titles are displayed using a standard format, with<br />

the first letter of each word capitalized (except for a few words that are commonly not capitalized in English titles).<br />

Rather than assume that the text file contains properly formatted titles, the application formats the titles while they’re<br />

being extracted from the text file.<br />

In the previous code listing, as part of extracting individual image metadata values, the following line of code is used:<br />

imageInfo.title = normalizeTitle(imageProperties[1]);<br />

In that code, the image’s title from the text file is passed through the normalizeTitle() method before it is stored in<br />

the ImageInfo object:<br />

private function normalizeTitle(title:String):String<br />

{<br />

var words:Array = title.split(" ");<br />

var l<strong>en</strong>:uint = words.l<strong>en</strong>gth;<br />

for (var i:uint; i < l<strong>en</strong>; i++)<br />

{<br />

words[i] = capitalizeFirstLetter(words[i]);<br />

}<br />

}<br />

return words.join(" ");<br />

This method uses the split() method to divide the title into individual words (separated by the space character),<br />

passes each word through the capitalizeFirstLetter() method, and th<strong>en</strong> uses the Array class’s join() method<br />

to combine the words back into a single string again.<br />

As its name suggests, the capitalizeFirstLetter() method actually does the work of capitalizing the first letter of<br />

each word:<br />

Last updated 6/6/2012<br />

22

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

Saved successfully!

Ooh no, something went wrong!