The String Data Type


The String Data Type

The string data type can be categorized as any amount of text between quotes. This includes characters, numbers, and certain properties of object instances such as the _name property of the movie clip object.

Creating a String

To create a string, place some text between quotation marks, as shown here:

 "this is a string literal"; "this is also a string"; //that was simple enough 

Another way of creating a string using the string data type is to declare a new string with the new constructor, which will make an instance of an object, in this case the String object:

 new String("this is a string literal"); 

Also, you can set a variable equal to a string (which we will discuss later in this chapter):

 var myString_str:String = new String("this is a string literal"); 

And you don't even need to use the new constructor; you can set a string to a variable like this:

 var myString_str:String = "another string"; 

TIP

Notice we use "_str" at the end of our string variable names. This is the assigned suffix for string data types and will activate code hints when they are turned on.


Empty Strings

You do not have to put anything between the quotes for it to be a string literal. You can just place an opening and closing set of quotes to create an empty string, as shown here:

 ""      // an empty string with double quotes ''      // an empty string with single quotes 

Although this is an empty string, it is not equal to the null or undefined data types. In the following example, we first start a new file by going to File in the menu bar, choosing New (PCCtrl+N, MacOpen Apple+N), and then choosing Flash Document. Then we use an if statement in the actions of the first frame in the main timeline and test it by going to Control in the menu bar and selecting Test Movie (if statements are discussed in Chapter 11, "Statements and Expressions"). Below is the ActionScript for the first frame.

 if ("" != null) {        trace ("An empty string is not equal to null"); } // output: An empty string is not equal to null 

Notice that we use a trace function that, when the movie is tested, displays our output in the Output panel. You'll see how to use this empty string, like the one found in the if statement, at the end of this chapter.

Quotes

As you have seen, all string literals must be surrounded by quotes. These quotes can be single quotation marks (') or double quotation marks ("), but you must close with the same type of quotation mark you started with. Here are some examples:

 "double quotes"      //legal string 'single quotes'      //legal string "double to single'   //illegal string 'single to double"   //illegal string 

If you do not close with the same quotation mark you opened with, you will receive this error:

 String literal was not properly terminated 

However, you can put quotation marks inside quotation marks, as shown here:

 'Then David said: "These are quotes inside a string"' //this is a legal string containing a quote within it 

You do need to be careful though, because a single quotation mark can also be used as an apostrophe. This can cause errors if you are not paying attention. Here's an example:

 'He wasn't going to go' //the interpreter reads this as 'He wasn' and throws an error message 

If we had used opening and closing double quotation marks instead of single quotation marks here, the interpreter would not have thrown the error. However, let's say we want to use single quotation marks anyway. In this case, there is a workaround: the escape sequence.

Escape Sequences

Escape sequences are string literals used with a backslash (\). This tells the interpreter to consider the following character or character representation as a character and not part of an action.

Here are some basic escape sequences:

 \"      double quote escape sequence \'      single quote escape sequence \\      backslash escape sequence using the backslash not as an escape sequence 

Let's take a look at our example again, but this time using the escape sequence:

 'He wasn't going to go' //as before, this will cause errors and not display the proper text 'He wasn\'t going to go' //now using an escape sequence, the problem is solved, and the //interpreter reads it correctly 

Now the interpreter reads the string correctly. Remember, you only have to use the quote escape sequences when you have quotation marks (double or single) that you want to display in between the opening and closing quotation marks of a string.

Now you have the basics of creating a string, let's go over how to manipulate them.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net