Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 删除as3中的空白_Actionscript 3_String - Fatal编程技术网

Actionscript 3 删除as3中的空白

Actionscript 3 删除as3中的空白,actionscript-3,string,Actionscript 3,String,如何在as3中删除字符串中的空白 我希望能够删除所有回车、空格、制表符等。您可以使用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; 除去空格和任何字符的最简单方

如何在as3中删除字符串中的空白

我希望能够删除所有回车、空格、制表符等。

您可以使用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;

除去空格和任何字符的最简单方法如下:

//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

如果您有权访问AS3 Flex库,那么还有
StringUtil.trim(“我的字符串”)
。对于文档


它并不完全符合OP的要求,但由于这是谷歌上关于AS3字符串修剪的最佳答案,我认为值得发布此解决方案,以满足更常见的Stringy trimmy要求。

已在iOS air应用程序的AnimateCC上测试并运行:

// 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);

如何创建自己的RegExp。有可用的TUT吗?这里的星号是错误的,因为星号也将匹配零长度字符串,如果您想用一个空格替换所有空格,它将无法按预期工作。改用加号-var rex:RegExp=/[\s\r\n]+/gim@Ofir:OP只问了关于移除的问题。您正在添加一个新条件,这是这个问题的一种范围渐变。我回答了那里的问题,并不是所有可能的问题。@Robusto:你的回答很好,但有点过火。空格不是零长度字符串,因此替换零长度字符串也不是问题的一部分。无论如何,将星号替换为加号会更好地回答这个问题,因为它将只替换空格。@Ofir:对于删除(再次,不是替换)空格,答案并不过分。假设您想要实现字符串的修剪函数。那么您肯定会使用
^\s*
\s$