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)
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!”.
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…
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());
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’.
myString = myString.toLowerCase();
myString = myString.toUpperCase();
These two functions will change the case of the entire string given.
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.
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.
Thanks! have you seen a way to calculate how many rows a textfield has? im trying to reveal each line one at a time but cant seem to figure out how that could be done.
Any help would be really helpful!
Also sorry trace(myString.charAr(9));
should be charAt?? The zip fla isnt available anymore? I’d love to see how you got the charAt method working i cant seem to.
Hi Tim thanks for the comments, yeah well spotted
it should be “charAt”. And the zip has been fixed.
What i would do is figure out how many are in a row (usually by counting em manualy) then use subString to extract up to that point from my string to send to the textfield/textarea.
I’ll try and work up an example later on for ya.
Hope this helps!
-Bill
Thanx a lot yeah that makes sense! I went to a AS3 conference laster in Chicago with Colin Moock, NOTHING made sense but reading over your articles im picking it up no problem! Thanx so much!
Ah Colin Moock, i want to get one of his books. Always good to get taught by a seasoned veteran
A better book to start with is Learning ActionScript 3.0 by Shupe and Rosser (O’Reilly). Awesome book for those getting their feet wet with AS3.
Yeah I’ve got that one. It’s pretty good to learn the basics, which you can never have enough of
[...] recorded first by pypons on 2008-09-25→ AS3 Basics: String Manipulation in Actionscript 3 [...]
nice blog, thanks