I’ve been developing apps in Flash Actionscript 2.0 (AS2) for about a 1/2 years now. But the other day i finally bit the bullet and decided to start learning Actionscript 3.0 (AS3). AS3 is the next big step in flash application development boasting a complete re-write with faster excecution speed, fully re-vamped event hadling system and classes work a hell of a lot like they’re supposed to (thank you Adobe!). Basicaly AS3 is pretty awesome.
In this set of tutorials i will be going over the basics of AS3, As a reminder to myself and to maybe help some people along the way. However be aware i’m not perfect and if you’re a seasoned programmer please point out my oddities
(Please Note i expect a basic understanding of programming syntax and variable types)
place this code on frame 1 of you’re time line.
var myString:String = "Hello this is my string! BEANS!";
function main():void
{
trace("--Main function ran--");
trace(myString);
}
main();
If you run you’re file now it should output “Hello this is my string! BEANS!”.
Code Explanation (skip if you already know you 1337 pr0gr4mm3r2):
var myString:String = "hello this is my string! BEANS!";
This line defines our string we are going to be manipulating. Notice ‘:String’ after the variable name. This is strict programming so that my variable ‘myString’ can not be populated with another variable type such as “int” or “Number”.
function main():void
defines our main function, Any statement within the braces {} will be excecuted upon calling the function.
trace("--Main function ran--");
trace(myString);
basic ‘trace();’ function displays text to flash’s output box. You can trace pretty much any type of data (very good for debugging).
main();
this last line calls our ‘main’ function and excecutes the statements within it.
Now on to the text manipulation…
Finding the length of characters in a string:
myString.length;
very simple, gets the length of the target string (all characters including spaces) much like an array starting from ’0′.
| h | e | l | l | o | t | h | i | s | i | s | m | y | s | t | r | i | n | g | ! | B | E | A | N | S | ! | |||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
NOTE: length is an “int” variable, thus it cannot be displayed in a text field, use ‘toString();’ to convert it to a string.
trace(myString.length.toString());
Finding a single character in a string using and index number:
myString.charAt(0);
This function will return a single character based on the index given just like an array index.
EXAMPLE
so if i were to use,
trace(myString.charAt(9));
the output would be ‘s’.
Changing the case of a string:
myString = myString.toLowerCase();
myString = myString.toUpperCase();
These two functions will change the case of the entire string given.
Extracting a selection from a string:
myString.substring(0,5);
Will extract from char ’0′ to char ’5′.
EXAMPLE
trace(myString.substring(0,5));
would output “Hello”.
NOTE: you instead of “substring(0,5)” you can use a shorthand of “substr(0,5)”.
NOTE: You can also use the ‘slice();’ function to do the same thing however it is better used with arrays.
Replacing characters in a string with another string:
myString.replace(0,"WEE"); replaces the targeted range of existing characters with the chosen string "WEE".
EXAMPLE
using in combination with ‘myString.substring(0,5)’ to find the range to replace.
trace(myString.replace(myString.substring(0,5),"WOO"));
the above would output “WOO this is my string! BEANS!”.
Well thats all the methods i know of at this time i’ve compiled them all into a .FLA demonstrating the usage of each.




