Home » ActionScript 3.0, AIR, Flash CS4, Flash CS5

Convert string to a character array + Flash Actionscript AS3

1 March 2011 6 Comments
Share on Facebook

How to do a String to character array conversion using Flash actionscript, as3. The String class has a neat method for doing this, split().
In the code example below the String is converted to a char array and each element of the array is printed out.

var str:String = "DesignScripting.com - A Flash ActionScript resource."

trace(str.split(''));

// Output : D,e,s,i,g,n,S,c,r,i,p,t,i,n,g,.,c,o,m, ,-, ,A, ,F,l,a,s,h, ,A,c,t,i,o,n,S,c,r,i,p,t, ,r,e,s,o,u,r,c,e,.

Note:
The output of str.split(”) is an Array.

Assign the output to and empty Array and lets see the output and try to access the character array by its index of 0

var str:String = "DesignScripting.com - A Flash ActionScript resource."
var arr:Array = [];
arr = str.split('');
trace(arr[0]);

// Output : D

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


6 Comments »

  • James said:

    This has been taught in schools already.

  • {code'r} said:

    Just what I needed – thanks.

  • admin said:

    welcome coder… :)

  • Arindam Mojumder said:

    Hello Sir,

    I can’t convert this String to an Array. I am fetching this string from XML.

    var srt:String = “{name:instance73,col:1278962},{name:instance79,col:5746957},{name:instance77,col:921102},{name:instance81,col:6316128},{name:instance75,col:15342859}”

    Waiting for your reply.

  • Arindam Mojumder said:

    Hello Sir,

    I can’t convert this String to an Array. I am fetching this string from XML.

    var srt:String = “{name:instance73,col:1278962}~{name:instance79,col:5746957}~{name:instance77,col:921102}~{name:instance81,col:6316128}~{name:instance75,col:15342859}”;

    var colarr:Array =(str.split(“~”));

    if I do this : trace(colarr[0].col);
    every time it is showing “Error #1069: Property col not found on String and there is no default value.”

    Waiting for your reply.

  • admin said:

    Hi,

    Here the code..

    var str:String = “{name:instance73,col:1278962}~{name:instance79,col:5746957}~{name:instance77,col:921102}~{name:instance81,col:6316128}~{name:instance75,col:15342859}”;

    var colarr:Array =(str.split(“~”));

    for(var count:Number=0;count

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.