Удалить пробелы в as3

Как удалить исключение из строки в as3?

Я хотел бы удалить все возвращаемые каретки, пробелы, вкладки и т.д.

Ответы

Ответ 1

Вы можете использовать RegExp.

var rex:RegExp = /[\s\r\n]+/gim;
var str:String = "This is            a string.";

str = str.replace(rex,'');
// str is now "Thisisastring."

Для обрезки передних и задних строк используйте

var rex:RegExp /^\s*|\s*$/gim;

Ответ 2

Если у вас есть доступ к библиотекам AS3 Flex, там StringUtil.trim(" my string ") тоже. См. здесь для документов.

Это не делает то, что было после OP, но поскольку это был лучший ответ на обрезку String для AS3 String, я подумал, что стоит опубликовать это решение для более обычного требования Stringy trimmy.

Ответ 3

Протестировано и работает над AnimateCC для приложения iOS для воздуха:

// Regular expressions
var spaces:RegExp = / /gi; // match "spaces" in a string
var dashes:RegExp = /-/gi; // match "dashes" in a string

// Sample string with spaces and dashes
loginMC.userName.text = loginMC.userName.text.replace(spaces, ""); // find and replace "spaces"
loginMC.userName.text = loginMC.userName.text.replace(dashes, ":"); // find and replace "dashes"

trace(loginMC.userName.text);

Ответ 4

Простейший способ удаления не только пробелов, но и любых char в этом случае состоит в следующем:

//Tested on Flash CS5 and AIR 2.0

//Regular expressions
var spaces:RegExp = / /gi; // match "spaces" in a string
var dashes:RegExp = /-/gi; // match "dashes" in a string

//Sample string with spaces and dashes
var str:String = "Bu  s ~ Tim  e - 2-50-00";
str = str.replace(spaces, ""); // find and replace "spaces"
str = str.replace(dashes, ":"); // find and replace "dashes"

trace(str); // output: Bus~Time:2:50:00