Here’s a simple, two line function for removing spaces (can be adapted for any other character) from a string in AS3. I used it for removing any spaces typed into a Post Code.
public function stripspaces(originalstring:String):String
{
var original:Array=originalstring.split(” “);
return(original.join(“”));
}
All we’re doing is splitting the array using the space character and then joining that array back together into a string with no delimiter, thus eliminating the space.


.gif)
{ 11 comments… read them below or add one }
Nice! Thanks for taking the time to write this and make it available!
be aware that when copying and pasting this code you will probably have to redo the ” as they go a bit funny due to the copy/paste
Thanks. This worked like a charm.
Very useful. Thank you very much!
I modified this to convert spaces to URL-encoded spaces (%20). Thanks for the help!
Much simpler:
myString.replace(/\s+/g, ”));
function strip(original, of_what )
{
return (original.split( of_what || ” “)).join(“”);
}
Probably a bit more useful …
Also the second, optional, argument can be either string or an regular expression …
–DBJ
I defy any C# “afficionado” to show me parallel to this, in her favourite language ;o)
if (“function” !== typeof “”.minus)
String.prototype.minus = function( what_ ) {
///
/// “ABCBDBEB”.minus(“B”), returns : “ACDE”
/// Argument is optional, by default it is one space char
/// myText.minus(), returns myText without spaces
/// argument can be an regular expression
/// reg.exp. given does not require ‘g’ or ‘m’ modifier
/// if argument is not found in the original, the original is returned
///
return (this.split(what_ || ” ” )).join( “” ) ;
}
–DBJ
Thanks!
Thank you so much for the tutorial!!! It worked flawlessly! I modified it so that it removed extra vertical spaces in an external text document.
Hi
I don’t know if someone else has mentioned this but you can use stuff like:
var inString:String = ” this has whitespace”;
var inString2:String = “this has no whitespace”;
var a:Array = inString.split(/^\s+/);
var b:Array = inString2.split(/^\s+/);
Any leading whitespace is removed , you can use different flags to adapt to your needs
Nice post though
All the best