// you’re reading...

Actionscript 3.0

AS3 Basics: String Manipulation in Actionscript 3.0

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 :P

(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.

Related Wuup Stuff:

You're Thoughts & Discussion

9 comments for “AS3 Basics: String Manipulation in Actionscript 3.0”

  1. 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!

    Posted by Tim | August 6, 2008, 1:40 pm
  2. 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.

    Posted by Tim | August 6, 2008, 2:21 pm
  3. Hi Tim thanks for the comments, yeah well spotted :P 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

    Posted by bigtallbill | August 6, 2008, 4:12 pm
  4. 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!

    Posted by Tim | August 11, 2008, 11:49 am
  5. Ah Colin Moock, i want to get one of his books. Always good to get taught by a seasoned veteran :P

    Posted by bigtallbill | August 12, 2008, 4:54 pm
  6. 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.

    Posted by Weezer | August 17, 2008, 5:43 am
  7. Yeah I’ve got that one. It’s pretty good to learn the basics, which you can never have enough of :P

    Posted by bigtallbill | August 20, 2008, 4:35 pm
  8. [...] recorded first by pypons on 2008-09-25→ AS3 Basics: String Manipulation in Actionscript 3 [...]

    Posted by Recent URLs tagged As3 - Urlrecorder | September 25, 2008, 2:47 pm
  9. nice blog, thanks

    Posted by Wipqjsim | October 2, 2008, 9:01 am

Post you're thoughts...