Next freebie in 28 Hours*
Let's study the addition of both strings and numbers. Numbers we can guess, but what happens when you add two strings? If you think that isn't enough, we'll even look at error 1084 and figure out how to avoid it.
We know how to add two integers using Flash: it's very easy and can be done using the '+' operators. To trace the addition of two numbers, there isn't much effort involved. But as we develop as programmers, we may have to add something more than just numbers. We need to perform additions on not only integers but Strings as well. You might wonder how and why we add strings. It's the same as adding numbers, but the strings to be summed up must be within double quotes as usual. Once you put a + sign between the two strings, the output is one string containing both the words/sentences together. The addition of strings (concatenation) is also performed using the '+' operator.
Many people might think that if the need is just to combine the words together, why can't we directly write two words together within the double quotes? This will be answered when we start dealing with some bigger programs where the two strings are needed separately but must also be combined later, so addition becomes a must. An easy example: one string that must be combined with only one of twelve: "My birthday is " and one of "Dec", "Jan", "Feb"... You now know how to use strings in variables. Your code could look like this:
trace("My birthday is"+ monthStr);
Variables add extra control in our creation process. We declare variables and assign them strings separately because these strings are needed in the program separately. Hence, we use the first variable wherever the first string is needed and second variable where the second string is needed etc. So now if we want a string which is the combination of these two, we don't waste time in declaring a new variable and assigning it the combined string. In fact, we add these two variables together to form the combined string as output.
The point that must be remembered is that addition is not just about adding numbers but strings as well. The good news is even through this is a new concept, its the exact same process we did for variables – only the outcome is two strings combined. An example taken from an ancient internet joke:
var strPet:String="Benoline"; // user enters the name of their first pet into String strPet
var strStreet:String="Programmer Blvd"; // user enters the name of the first street they lived on into String strStreet
trace("Your stripper name is " + strPet + strStreet);
Have a question but don't want to ask it publicly? If you are a prime member just ask right here and we will get back to you within 48 hours. Not prime yet? no worries you can ask publicly below.