ActionScript String formating with Sprintf
ActionScript only provides some basic but powerful methods for string handling. By combining them you can create almost every functionality you need when working with character based data. Sprintf is a class that offers string formatting as known from other programming languages like Perl or C.
According to a format, Sprintf produces an output based on a given character string. The format is composed of zero or more directives: ordinary characters which are copied unchanged to the output and format specifications which results in fetching some arguments passed to the method.
Each format rule is introduced by the % character followed by some modifiers:
f --> float
s --> string
o --> octal
h, H --> hexadecimal
b --> binary
The example mixes the string with arguments:
var format:String = "There are %d monkeys in the %s.";
Sprintf.format(format, 12, "house"); // result: There are 12 monkeys in the house.
The class also allows some more advanced formatting of numbers and values:
var format:String = "%04d-%02d-%02d";
Sprintf.format(format, 2006, 1, 9); // result: 2006-01-09
var format:String = "Result: %02.2f";
Sprintf.format(format, 3/2); // result: Result: 01.50