by
kirupa | 3 April 2006
In the previous page, you separated our input by differentiating
between the whole dollar and the cents based on the position of
the decimal point. I have not explained how the separation
actually works. I will do that and more on this page!
Let's now take a look at what each new line of code you pasted
from the previous page does.
- var decimalIndex:Number
= inputString.indexOf('.');
The indexOf method takes in a particular character as its
argument and returns the numerical position of where it is
located in our string.
The indexOf method always returns a number. It returns the index
position of the first occurrence of the character inputted or a
-1 if the string does not contain the character at all. In the
above line of code, the string in question is
inputString, and
our indexOf method checks to see where the decimal point, the .
character, occurs in our string.
You can think of a string as a collection of individual
characters each at a particular, numerical position:
The first and only occurrence of the decimal point occurs at
index position 7. If you trace the decimalIndex variable, you will see the number 7
displayed as the output from the indexOf method.
Let's look at the next line of code:
- var centString:String
= inputString.substring(decimalIndex,
decimalIndex+3);
In this line of code, we store the two characters after our
decimal point into our centString variable. I am able to do this
easily by using the subString method. The subString method takes
the starting index position and the ending index position
plus one of our
string and returns the characters in between the starting and
ending positions.
In the code above, the range of characters lies between
decimalIndex and decimalIndex + 3.
One way of looking at it would be to think of the
subString function boxing in the
characters it wants. That would also explain why have to
increment your final character's index position by 1:
So, the data stored inside centString based on the
subString
operation will be .44.
- var dollarString:String
= inputString.substring(0,
decimalIndex);
We now have our cent information parsed out of our string. The
above line of code takes care of everything else. The
dollarString variable stores all the characters between
inputString's starting position, index position 0, and the ending
position up to the decimal point, decimalIndex:
Phew. That was a lot of explanation for what looks like a simple
task! Luckily, the next three lines are simple variable
declarations:
- var finalString:String
= "$";
- var count:Number
= 0;
- var tempString:String
= "";
We are declaring three variables: two String objects and one
Number object. Let's focus on the two String objects. If you
look through the code, you will see that I declare a lot of
String objects. It almost seems like an unnecessary amount. The
reason I do that is, in Flash and several other OOP languages
such as C# and Java, String objects are unchangeable aka
immutable. Once I initialize a String object, I
cannot change it by adding more characters to it. I have to work
around that by declaring new String objects that contain the
original string and an operator that performs any
string manipulation that I wish to accomplish.
For example, I cannot do the following:
- var firstName:String
= "kirupa";
- firstName +
" says hello!";
- trace(firstName);
My output would still be kirupa instead of "kirupa says hello." I can, instead do two things to
accomplish the same goal:
- // Thing 1
- var phrase:String
= firstName
+ " says hello!";
- trace(phrase)
-
- // Thing 2
- firstName +=
" says hello!";
Both of the above approaches, which I cheekily call Thing 1 and
2, end up tracing "kirupa says hello!" You might be wondering
why Thing 2 works, but remember the += operator is the
short-hand version of writing firstName = firstName + "
saysHello". Technically, I am simply overwriting the old
variables contents with a copy of the old variable data along
with the new data.
Ok, I think I have sidetracked a little too much. Anyway,
hopefully you have an understanding of why I am being a bit
redundant by using many variable names in my code. In the next
page, I will explain the rest of the code and wrap up the
tutorial!
Onwards to the next page!
|