Search code examples
actionscript-3apache-flex

Split textfield value in 2 parts and put each part in a var


I have a textfield, that has a text value: France Paris. Now I need to know how I can take that string, cut it in 2 parts (France and Paris) and put those two parts in a var. So:

<textfield id="field1" text="France Paris">

and somewhere i should get

var1 = France;
var2 = Paris;

I know there is a split string command, but I'm not familiar with any of this stuff.


Solution

  • For this you could use the split() method on your String. This will split your String and put the values in an Array. You can use it like this:

    var yourString:String = "France Paris";
    var splitString:Array = yourString.split(" ");
    
    var firstWord:String = splitString[0];  
    var secondWord:String = splitString[1];
    

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#split()