Can anyone suggest a way to remove extra whitespaces from a string that exists in a Flash variable?
March 24, 2004 7:29 AM   Subscribe

Can anyone suggest a way to remove extra whitespaces from a string that exists in a Flash variable? [more inside]

I'm messing around with a Flash animation that started out as a JavaScript. It generates a nonsensical phrase from sentence fragments, but the result may have multiple whitespaces in it, and I need to remove them (unlike the JavaScript version, which automatically discards multiple whitespaces...).

I think maybe the condenseWhite method may do this, but I can't figure out a simple way to implement it. Has anyone done this before?
posted by jpburns to Computers & Internet (9 answers total)
 
This will do it for ya:
String.split(" ").join("");

Replacing String with whatever your variable is called.
posted by malphigian at 7:46 AM on March 24, 2004


Won't String.split(" ").join(""); remove ALL spaces from a string? If you just want multiple spaces converted to single spaces try this:

s = "       This is a test     of the program.    ";
//
String.prototype.removeExtraSpaces = function() {
tmp = this;
while (tmp.indexOf("    ") != -1) {
found = tmp.indexOf("    ");
tmp = tmp.substr(0, found)+tmp.substr(found+1);
}
// deal with a possible stray at the start of the string
if (tmp.substr(0, 1) == " ") {
tmp = tmp.substr(1);
}
// deal with a possible stray at the end of the string
if (tmp.substr(tmp.length-1, tmp.length) == " ") {
tmp = tmp.substr(0, tmp.length-1);
}
return tmp;
};
trace("["+s.removeExtraSpaces()+"]");
posted by gwint at 7:58 AM on March 24, 2004


// only tested as javascript - don't have flash on this machine - methods exist in both
while (aString.indexOf("  ") != -1) {
    var anArray = aString.split("  ")
    aString = anArray.join(" ")
}
posted by TimeFactor at 8:56 AM on March 24, 2004


move (replaces(" ",sTmpString," ")) to sTmpString
oh... Wait...
You said Flash, not the programming Language From Hell, that I've managed to destroy my C.V. with.
posted by seanyboy at 10:02 AM on March 24, 2004


Response by poster: Thanks, all. This seems to work with my variable called "outPut"(adapted from TimeFactor's version):

function fixIt(){
anArray =outPut.split(" ");
return(anArray.join(" "));
}


(this is previewing OK, I may need to fix after actually posting...)
posted by jpburns at 10:28 AM on March 24, 2004


TimeFactor: Your method works great, unless there is space at the beginning of the string.
posted by gwint at 10:36 AM on March 24, 2004


jpburns: If you know you'll never have more than two spaces in a row your version is preferable. Otherwise, you'll need the while loop.
gwint: yup. or at the end.
posted by TimeFactor at 11:16 AM on March 24, 2004


Response by poster: Yep... but for some reason the "while" method made Flash Player beachball, and then inform me that something in my code was taking a lot of time...

... since I'm making the pieces that get connected together, it's pretty easy to be assured that I won't have more than two spaces together.

Thanks, again. It really helps to see an example like that.
posted by jpburns at 11:32 AM on March 24, 2004


I've never used it, but someone wrote a reg exp. class for flash. It might be worth looking into.
posted by alana at 12:10 PM on March 24, 2004


« Older OS X coders, what is your freeware text editor of...   |   Should I continue to invest in SD accessories? Newer »
This thread is closed to new comments.