02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Mixing Strings and Numbers

When working with strings and numbers it's important to prevent Flash from automatically converting data so you'll not be automatically confused. To keep things simple it's best for you to manage your conversions on your own. Let's learn why and how.

We have seen how to add two strings together using Flash, after viewing the previous video. As programmers, we need to learn all the possible operations that take place in our domain (Flash.) So we have to be careful while dealing with numbers and strings together. There are occasions where we need to add a number to a string or vice versa, so now we'll learn all the possible ways to mix stings and numbers together.

As usual, this type of addition is performed using the '+' operator. We can type the string and then add a number to it, for example

trace('Flash' + 1);

This will make the compiler assume 'Flash' is a String and 1 is a Character. Addition is therefore performed: the output is the String "Flash1". The process is the same even if the values are interchanged: for instance, trace(1 + 'Flash'.) Here the output will be "1Flash" (also considered as a string.) We can add any number to a string without putting the number in double quotes.

There is a sneaky problem here! When we add a string and a number, the application assumes the number is a character and then adds (concatenates) it to the String. This might lead to problems when we don't really want the application to take the number as a character. It becomes more complicated when two numbers are added together and one of them is in double quotes: trace("20"+10.) Flash assumes both the numbers as strings and combines them instead of adding the two: here, you get "2010" as your string result.

Moreover, when part of the equation has several numbers and part of it has a string, then addition is performed for some parts and the string part is combined to that result. For example, if we take trace(1 + 1 + 3 +'2'), we expect the result as 7. But we now know the result is altered due to those double quotes: we expect the result to be the combination of all the numbers, 1+1+3 added to the string "2". Sure enough, Flash adds the first three numbers (not in double quotes) resulting in the sum equal to 5. Now, this integer is combined to the string "2'' which yields "52" as output (also considered as string.)

It's very difficult to understand whether the number is considered as integer or string. However, effort, concentration and practice will make any task easier. The good news is that a few exams added to all that will do the trick – plus perhaps an email or two to us if you get lost!

Video Transcript

00:00Let's talk about what would happen if you add a string and a number together.
00:04So if I trace out, for example, 2 plus string, value of string,
00:14and I run the application, let's see what will happen.
00:18So Flash automatically recognizes that it were trying to add a number and a string together,
00:24and it will automatically convert our number into a string.
00:29Now what would happen if we would go the other way around?
00:32If we would trace string plus 2.
00:42Now obviously, we don't really want to do things like this,
00:44and we could see that that works as well.
00:47Basically, when we're working with Flash, we want to avoid scenarios
00:53where we're letting Flash do our conversions for us.
00:56The reason we want to avoid it is because a lot of times
00:59these automatic conversions could become confusing,
01:03such as what would happen if we would trace 2 plus 2?
01:11What value would come out?
01:12Would it come out as 4, or would it come out as 22?
01:17And let's see.
01:19And the value that will come back is 22.
01:21And the reason is because Flash is converting our second number into a string as well,
01:27and it's disregarding the fact that it's a number.
01:30So if we thought that we would get 4, we did it,
01:35and Flash automatically converted our number into a string.
01:43What would happen in this scenario?
01:46And you can see that things start getting confusing.
01:49So 2 plus 2 equals 4, which is a numeric value,
01:52and then it turns our number into a string
01:55and converts it into a bigger string, which is 42,
02:00which is not really what we were planning on getting or wanting to get.
02:03So let's see.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!