Programming Primer
Licensing And Preface

Standardized Coding Practices

Errors And Exceptions


String DataType

Number DataTypes


Commonly Used Flash Commands

More Topics:





Part 5: String DataType

What are Strings?

Strings are literal values that usually denote some sort of descriptive meaning using alphanumeric or punctuation characters. They can also contain several elements for example the string “John” is made up of a J, o, h and an n. Strings can also contain white space characters such as a space or a tab, eg “ “is a string. This is also considered as an element in exactly the same way a “J” or an “o” for example is also considered a legal element of a string. When using strings in ActionScript the elements of that string must always be enclosed within quotation marks. The quotation marks must either be double or single, and always remain consistent that is to say that if you start a string with double quotation marks, you must end the string in the same way. Mixing double and single quotes will lead to errors. There is, however, an exception; if you wanted to include quotation marks as one or two of the elements making up your string you would start and end your quotation marks denoting a string value in the same way, then use the other quotation pair within the actual string. For example,

trace("John is the same as 'Jon'");

Concatenation of Strings

Concatenation is the process of linking together various things. In our case we are going to discuss concatenating strings. The concatenation operator for strings as you might have already guessed is exactly the same operator used in math, the plus sign “+”. to join several strings together we could use the operator like so,

trace("Hello" + " my name" + " " + "is Jon");

Notice this trace statement concatenates four strings together. It's not necessary to use as many strings as I did in this example but I think this code illustrates my point more clearly.

This method of concatenating strings is okay, but a bit difficult to follow and not very flexible at all. A more commonly used technique would be to use variables data typed as strings then add the variables together. You will see this method of concatenating strings in code used more often by experienced programmers. That doesn't mean that if you don't consider yourself to be an experienced programmer that you should not use the technique. The example above is not very versatile if I wanted to repeat the statement it would mean having to include the whole line of code again. Although this might not seem like a major issue at this stage because the code is still very simple (even repeated), however when writing larger scripts repetitions code like this can make your code less human readable and also execute slower. It is therefore within your interests to consider changing and assigning values indirectly through variables for example,

var greeting:String = "Hello, my name is ";

var name:String = "John";

greeting = greeting + name;


This code only uses two variables and is versatile in the sense that we could customize the name variable to any name we want and not have to worry about updating anything else in the script. Repeating the script is easy, as it's just a matter of typing a single word. Indirect manipulation of variables is something you will find invaluable when you start writing your own scripts.

Next: Numbers Data Type