I like python and the ability to do variable substitutions. In AS3 you have to do… well see the example below. Alas, this is for AS2, not AS3.
sprintf : Downloads : Nate Cook
Formatted strings are a real convenience—one that is sorely missing
from the Flash Actionscript library. This adds in the sprintf
functionality, which lets you reduce something like this:
var m,d;var myDate = new Date();
m = myDate.getMonth() + 1;if (m < 10) m = '0' + m;
d = myDate.getDate();if (d < 10) d = '0' + d;
trace(m + '-' + d + '-' + myDate.getFullYear());// traces out: 02-14-2004
into basically a single line of code:
var myDate = new Date();
Sprintf.trace('%02d-%02d-%4d',myDate.getMonth() + 1, myDate.getDate(),myDate.getFullYear()));// traces out: 02-14-2004
Ah! But! Someone did kindly make an AS3 version.
Sprintf for AS3?
AS2 and AS3 are similar, but incompatible. You’d have to spend some time tweaking the AS2 version you have to get it to compile in AS3, so just try Manish’s AS3 version instead: http://tasmania.globat.com/~mannu.info/flex/sprintf.as
But… the link is dead… there is mention of an ASToolBox on the web but I don’t know which version that works with. Ah… hmm… just a sec… nope… dead links… how about this? Bingo. OK so there it is.
But another one also turned up in the search for “actionscript 3 printf”. This could be very useful!
printf-as3 – Google Code
Inspired by python’s print and strtime.
This is really a simple project. Inside the rep, souce, docs and examples.
Some basic usage:
import br.com.stimuli.string.printf;// objects are substitued in the other they appear
printf("This is an %s lybrary for creating %s", "Actioscript 3.0", "strings");// outputs: "This is an Actioscript 3.0 lybrary for creating strings";// you can also format numbers:
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"// Instead of positional (the order of arguments to print f, you can also use propertie of an object):var userInfo : Object = {"name": "Arthur Debert","email": "arthur@stimuli.com.br","website":"http://www.stimuli.com.br/","ocupation": "developer"}
printf("My name is %(name)s and I am a %(ocupation)s. You can read more on my personal %(website)s, or reach me through my %(email)s", userInfo);// outputs: "My name is Arthur Debert and I am a developer. You can read more on my personal http://www.stimuli.com.br/, or reach me through my arthur@stimuli.com.br"// you can also use date parts:var date : Date = new Date();printf("Today is %d/%m/%Y", date, date, date)
For a full list of available formaters see : http://stimuli.com.br:8080/media/projects/printf-as3/docs/printf/as/package-detail.html