12

I searched it for while but didn't find anything like printf in Action Script.

That makes it a little difficult to generate formated strings.

Reunanen
  • 7,921
  • 2
  • 35
  • 57
faceclean
  • 3,781
  • 8
  • 39
  • 58

4 Answers4

8

Printf-as is a third-party library that will handle this. Here is the GitHub repo. From the README:

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"

It also plays well with dates:

var date : Date = new Date();
printf("Today is %d/%m/%Y", date, date, date);
vitaLee
  • 155
  • 1
  • 6
7

think you might be looking for StringUtil.substitute()

take a look here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/StringUtil.html#substitute

o0'.
  • 11,739
  • 19
  • 60
  • 87
Acatl Pacheco
  • 79
  • 1
  • 1
  • broken link, try this one: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/StringUtil.html#substitute() – o0'. Dec 01 '10 at 14:52
  • that said, I don't like it: it has none of the useful features of printf, and it offers no feature more than a simple string concatenation with `+` could do. – o0'. Dec 01 '10 at 14:54
  • While StringUtil does not have the number formatting capability of `sprintf`, it is useful in that it allows separating the template generation from the formatting action that is also a useful feature of `sprintf`. And the fact that it is builtin and does not require external code is a large plus in my book. – Guss Jan 29 '12 at 18:33
1

There is the Formatter classes that you could use or create custom formatters.

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

  • 1
    the mx.formatters.Formatter is a generic "lets manipulate strings" base class that has none of the capabilities of `sprintf()`, not even the field substitution a-la Java's MessageFormat, that `StringUtil` has (from @Acatl Pacheco's response). Its also not really ActionScript generic - I doubt if it makes sense outside MXML. – Guss Jan 29 '12 at 18:30
-1

The only function I know that prints to a standard output is trace() (debug purposes only) , which can take virtually any type of variable.

I might not have understood your question, though.

Isaac Clarke
  • 717
  • 1
  • 9
  • 19
  • 1
    Actually, I mean something like "sprinf". I don't need to send text to console. I need to generate formatted strings. – faceclean Jul 29 '09 at 07:32
  • What for ? On a TextField ? Typing is very limited in AS3 (Number, int, String, basically), I think you may want to give us more details about what you want to do. – Isaac Clarke Jul 29 '09 at 08:08