Search code examples
flashvariablessplitactionscript-2

Split Flash AS2 array into individual variables


My string: s5_1
Basically, I want to split the string so that it results two variables, one as "s5" and the other "1".

For example

variable1 = "0";
variable2 = "0";
var mystring:String = "s5_1";
//SPLIT CODE HERE
trace(variable1); //this will trace "s5"
trace(variable2); //this will trace "1"

Thanks for your help.


Solution

  • The split method of String will split a string into an array of strings based on a delimiter:

    variable1 = "0";
    variable2 = "0";
    var mystring:String = "s5_1";
    var parts = mystring.split("_");
    variable1 = parts[0];
    variable2 = parts[1];
    trace(variable1); //this will trace "s5"
    trace(variable2); //this will trace "1"