Strings+in+ActionScript

=Learn about Strings=

Strings are a series of characters.

The computer stores each character using a numerical code. The code is defined by the ASCII system. 1. What does ASCII stand for? 2. What is the range of the codes for ASCII characters?
 * American Standard Codes for Information Interchange**
 * 0-255**

Each character has an index.
 * h || e || l || l || o || ! ||
 * 0 || 1 || 2 || 3 || 4 || 5 ||

3. What is the index of the first character in every string?
 * 0**

Strings in ActionScript
Strings are primitive types in ActionScript, like int, Number, or Boolean.

Creating Strings
You can create a new string two different ways:

code format="actionscript3" var fruit:String = "strawberry"; code
 * Method 1**

code var spread:String = new String(“Peanut Butter”); code
 * Method 2**

You will see the keyword new used later in the course.

What can we do with Strings?
To find the length of a String (the number of characters in the String), we use .length as in: code format="actionscript3" trace(fruit.length); code

4. What does this print out?

.length is an example of a **property** of the String

You can also do things to the String, such as get it in lower case by using .toLowerCase, as in: code format="actionscript3" trace(spread.toLowerCase); code

Similarly, you can get a String in all upper case, using .toUpperCase.

Here are some other things you can do with a String: code format="actionscript3" trace(spread.charAt(3)); code
 * Get one character, by giving the computer the index, using .charAt(index)

code format="actionscript3" trace(fruit.charCodeAt(4)); code
 * Get the ASCII code for a character, using .charCodeAt(index)

code format="actionscript3" trace(fruit.indexOf(“r”)); trace(fruit.indexOf(“q”)); code
 * Find the first occurrence of a letter, using .indexOf(letter)

code format="actionscript3" trace(spread.substring(1, 4)); code
 * Get a piece of a String, using .substring(start, finish)[[image:i/c.gif width="23" height="21"]]

code format="actionscript3" if(fruit == spread) { trace(“Your fruit and your spread are the same!”); } else { trace(“Your fruit and spread are different”); } code code format="actionscript3" var str1:String = “Jolly Green”; var str2:String = “ Giant”; var str3:String = str1 + str2; trace(str3);
 * Compare if two Strings are equal (using ==) or not equal (!=)
 * Join two Strings together (called concatenation ) by using the + operator, as in:

code 5. Add comments in your code (from above) to explain what each operation does.

You can find out more about String variables from the Flash documentation at: []