Flash / AS

Blend / WPF
ASP.net / PHP

Photoshop

Forums
Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

Using Strings
       by kirupa  |  3 April 2006

Have you ever thought about the text that you are reading on your computer? More than likely not, and that's good. There are far better things to do with one's time. But, as a programmer, you will find yourself having to display textual information to the screen. You might be expected to generate text based on what your program is doing, and that is often more complicated than simply displaying pre-formatted text. Essentially, you now have to figure out how text is used in Flash and what things you can do to manipulate your text.

Everything you display on your screen is nothing more than a series of small characters. Usually, these characters are letters or numbers. A collection of such small characters is called a string.

In Flash, you have the String class that takes care of many of the grungy details associated with making something a string. If you are not familiar with OOP concepts such as class/methods/objects, you may want to give senocular's OOP Tutorial a whirl first!

You can declare variables as String objects by following your variable name with the String type:

var stringData:String = "Hello!";

Because stringData is a variable of type String, it has access to all of the built-in methods (features) of the String class. Many common functions that you would want to use with strings have already been developed for you by the Flash developers. The full list of String methods you can use can be found by clicking here [external link].

In this tutorial, I will explain how to use the String class and many of its methods to create a dollar amount parser. By the end of this tutorial, you will have learned how to think about strings as a series of characters, and how to manipulate them using the String class's methods.

Getting back to our tutorial's goal, the dollar amount parser is a function that takes in a number and returns a dollar amount complete with the dollar sign ($), commas (,), and the right number of digits after the decimal point (.).

Let's build our dollar amount parser incrementally. That allows you to get a feel for how to use strings if you are just starting out with ActionScript, and it helps me to explain more complicated concepts by making references to earlier, easier topics.

Just for reference, you will find our full code below. Don't worry about the code now, though, for you will incrementally be introduced to the code in the tutorial:

dollarParser = function () {
var input:Number = 1567.5644;
var inputString:String = input.toString();
var decimalIndex = inputString.indexOf('.');
var centString:String = inputString.substring(decimalIndex, decimalIndex+3);
var dollarString:String = inputString.substring(0, decimalIndex);
//
var finalString:String = "$";
var count:Number = 0;
var tempString:String = "";
for (var i:Number = dollarString.length-1; i>=0; i--) {
count++;
tempString += dollarString.charAt(i);
if ((count%3 == 0) && (i - 1 >= 0)) {
tempString += ",";
}
}
for (var k:Number = tempString.length; k>=0; k--) {
finalString += tempString.charAt(k);
}
finalString += centString;
trace(finalString);
};
dollarParser();

The above code might not make much sense, but in the next few pages, you will realize that all of this isn't so complicated after all!

Onwards to the next page!

1 | 2 | 3 | 4


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components

Flash Templates
CSS Templates
Dreamweaver Templates

flash menus, buttons and components
Digicrafts Components The best flash components ever!
Entheos Flash Website Templates Free Flash Page Flip
flash components Buy and sell FLAs at Ultrashock!
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com create flash slideshow as easy as 1,2,3
Learn how to advertise on kirupa.com