AS3 – Remove Spaces from a String

I had a need to remove spaces from a string the other day so I came up with this helper utility and put it in a small Utility class. This could also be used to remove other characters.

1
2
3
4
5
6
7
8
9
10
package {
 
	public class Utility {
 
		public static function removeSpaces(str:String):String{
			var text_arr:Array=str.split(' ');
			return(text_arr.join(''));
		}
	}
}

Usage:

1
2
3
4
import Utility;
 
trace(Utility.removeSpaces('Remove All Spaces Please.'));
//Output:  RemoveAllSpacesPlease.
  1. This can be accomplished using replace also:
    var myPattern:RegExp = / /g;
    str = str.replace(myPattern, “”);

    ukw.

  1. No trackbacks yet.

 

Switch to our mobile site