How to Remove Spaces from a String

Removing spaces from a string is really easy in Actionscript 3. One method is to simply convert the string to an array using the String.split() function using a space as the delimiter. Then you can use the Array.join function to put it all back together again without the spaces.

Here is a simple function that will do the trick:

public function removeSpaces(input:String):String
{
    var split:Array = input.split(" ");
    var output:String = split.join("");
    return output;
}

That’s it!

example:

trace(removeSpaces("ABCD EFG HI    JKLMNOP QRSTU      VWXYZ"));
// outputs: ABCDEFGHIJKLMNOPQRSTUVWXYZ

Leave a Reply