string 删除 as3 中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2692365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:42:50  来源:igfitidea点击:

Remove whitespace in as3

actionscript-3string

提问by Derek Adair

How can one remove whitespace from a string in as3?

如何从 as3 中的字符串中删除空格?

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

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

回答by Robusto

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;

回答by Ted

If you have access to the AS3 Flex libraries, there's StringUtil.trim(" my string ")too. See herefor the docs.

如果您可以访问 AS3 Flex 库,也可以访问StringUtil.trim(" my string ")有关文档,请参见此处

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.

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

回答by Bob Smolenski

Tested and works on AnimateCC for iOS air app:

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

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

回答by Rajneesh Gaikwad

The simplest way of removing not only spaces but any char for that matter, is as follows,

不仅删除空格而且删除任何字符的最简单方法如下,

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