How can one remove whitespace from a string in as3?
I would like to be able to remove all carriage returns, spaces, tabs etc.
You can use RegExp.
var rex:RegExp = /[\s\r\n]+/gim;
var str:String = "This is a string.";
str = str.replace(rex,'');
// str is now "Thisisastring."
For trimming front and back of strings, use
var rex:RegExp /^\s*|\s*$/gim;
If you have access to the AS3 Flex libraries, there's StringUtil.trim(" my string ")
too. See here for the docs.
It doesn't do exactly what the OP was after, but as this was the top answer on google for AS3 String trimming, I thought it'd be worth posting this solution for the more usual Stringy trimmy requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With