Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove whitespace in as3

How can one remove whitespace from a string in as3?

I would like to be able to remove all carriage returns, spaces, tabs etc.

like image 914
Derek Adair Avatar asked Apr 22 '10 15:04

Derek Adair


2 Answers

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;
like image 154
Robusto Avatar answered Oct 11 '22 14:10

Robusto


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.

like image 29
Ted Avatar answered Oct 11 '22 13:10

Ted