Using Strings - Page 2
       by kirupa  |  3 April 2006

In the previous page, I introduced strings and how all string objects are extensions of the String class in Flash. In this page, we will start deconstructing the previous code in order to create our dollar parser.

Let's get started!
Create a new animation in Flash. This tutorial will be all code-based, so right click on the empty keyframe in your timeline and select "Actions". Your actions window should appear:

Let's first create the function to house all of our code. Copy and paste the following code into your Actions Window.

dollarParser = function () {
trace("Hello!");
};
dollarParser();

In the above lines of code, I create a new function called dollarParser and add a trace action inside. I immediately make a call to our dollarParser function. If you test this animation by pressing Ctrl + Enter, you will see the "Hello" text display in our Output window:

Because it is difficult to visualize code output if you are just starting out with programming, I will be using trace actions frequently at the beginning. In this case, I added a trace text simply to show you that our dollarParser() call (line 4) calls our dollarParser function.

Let's declare some variables! Replace our above dollarParser function with the following code:

dollarParser = function () {
var input:Number = 1234567.5644;
var inputString:String = input.toString();
}

I declare two variables - input and inputString. In order to work with strings, all of our variables need to be in the String format. From the above lines of code, our input variable is a Number object. We need to convert input into a String. That is where the second variable inputString comes in. I use the toString() method on our input variable to convert our Number data into a string.

With our input now stored as a String in the inputString variable, let's continue. First, let's work on getting our function to deal with the decimals properly. For this tutorial, for any input, there should be only two numbers after our decimal point. In other words, 45.23 is acceptable whereas 68.334 is not acceptable.

In the context of our code right now, to recap, we want our number 1234567.5644 to be 1234567.56. The extra 44 after the decimal point is not necessary. We accomplish that with three extra lines of code added to our dollarParser function:

dollarParser = function () {
var input:Number = 1234567.5644;
var inputString:String = input.toString();
 
var decimalIndex:Number = inputString.indexOf('.');
var centString:String = inputString.substring(decimalIndex, decimalIndex+3);
var dollarString:String = inputString.substring(0, decimalIndex);
trace(dollarString);
}

Be sure to add the three colored lines of code in the right location in your Actions panel. Test your movie in Flash. Notice that your Output Window displays the expected data from our input variable: 123456.

In the next page, I will explain what each line of your newly pasted code does!

1 | 2 | 3 | 4




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.