在Action Script 3中有没有类似printf的东西?

问题描述:

我搜索了它,但在Action Script中没有找到像printf这样的东西。在Action Script 3中有没有类似printf的东西?

这使得生成格式化字符串有点困难。

Printf-as是一个第三方库,可以处理这个问题。 Here是GitHub回购。自述:

printf("You can also display numbers like PI: %f, and format them to a fixed precision, 
     such as PI with 3 decimal places %.3f", Math.PI, Math.PI); 
// outputs: " You can also display numbers like PI: 3.141592653589793, 
// and format them to a fixed precision, such as PI with 3 decimal places 3.142" 

它还日期打得很好:

var date : Date = new Date(); 
printf("Today is %d/%m/%Y", date, date, date); 
+2

另外http://code.google.com/p/printf-as3/ – 2010-02-19 21:05:30

+0

不幸的是它太有用了,像`%-12s`这样的东西不起作用。 – 2010-12-01 15:25:11

我知道打印到标准输出的唯一函数是trace()(仅用于调试目的),它实际上可以采用任何类型的变量。

虽然我可能不理解你的问题。

+1

其实,我的意思是像 “sprinf”。我不需要发送文本到控制台。我需要生成格式化的字符串。 – ablmf 2009-07-29 07:32:03

+0

什么? 在TextField上? 在AS3(数字,整数,字符串,基本上)中,键入是非常有限的,我想你可能想给我们更多关于你想要做什么的细节。 – 2009-07-29 08:08:32

有,你可以使用或创建自定义格式的格式化类。

http://livedocs.adobe.com/flex/3/html/help.html?content=createformatters_2.html

想你可能会寻找StringUtil.substitute()

到这里看看: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/StringUtil.html#substitute