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

Flash Actionscript 3: Easily Jumble elements of an Array or String

Sometimes in Actionscript you need an easy way to jumble elements of an array or string, such as for a card/word game. I created this simple little as3 class that you can use for just that:

// Usage:
// yourNewString = Jumble.string(yourOldString);
// yourNewArray = Jumble.array(yourOldArray);

package com.frigidfish{

    public class Jumble {

        public function Jumble() {}

        public static function string(s:String):String {
            var a:Array = s.split("");
            var r:String = "";
            a = a.sort(randomize);
            for(var i=0;i<a.length;i++){
                r += a[i];
            }          
            return r;
        }

        public static function array(a:Array):Array {
            a = a.sort(randomize);
            return a;
        }

        private static function randomize(objA:Object, objB:Object):int {
            return Math.round(Math.random()*2)-1;
        }
    }
}

Usage:

import com.frigidfish.Jumble;

var string:String = "abcdefghijklmnopqrstuvwxyz";
var array:Array = ["a","b","c","d","e","f","g","e"];

trace(Jumble.string(string));
trace(Jumble.array(array));

/*
possible output:
zvepdtnhijylarowksqfuxgcbm
e,c,e,d,a,f,g,b
*/

This is a useful couple of tools.. Maybe you could add functions to return the output in different formats? Above all, have fun with Flash Actionscript 3!