| Package | system.formatters |
| Class | public class StringFormatter |
| Inheritance | StringFormatter Object |
| Implements | Formattable |
Replaces the pattern item in a specified String with the text equivalent of the value of a specified Object instance.
Formats item : {token[,alignment][:paddingChar]}
If you want to escape the "{" and "}" chars use "{{" and "}}"
Example :
import system.formatters.StringFormatter ;
import system.evaluators.DateEvaluator ;
import system.evaluators.EdenEvaluator ;
import system.evaluators.MathEvaluator ;
var formatter:StringFormatter = new StringFormatter() ;
var result:String ;
// indexed from the arguments
formatter.parameters = ["big", "the"] ;
trace( formatter.format( "hello {1} {0} world" ) ) ; //"hello the big world"
formatter.parameters = [41] ;
trace( formatter.format( "Brad's dog has {0,6:#} fleas." ) ) ;
formatter.parameters = [12] ;
trace( formatter.format( "Brad's dog has {0,-6} fleas." ) ) ;
formatter.parameters = ["a", "b", "c", "d"] ;
trace( formatter.format( "{3} {2} {1} {0}" ) ) ;
// named from an object
formatter.parameters = [ { name : "HAL" } ] ;
trace( formatter.format( "hello I'm {name}" ) ) ; //"hello I'm HAL"
// indexed from an array
var names:Array = ["A","B","C","D"];
var scores:Array = [16,32,128,1024];
for( var i:int=0; i$lt;names.length; i++ )
{
formatter.parameters = [ names[i], scores[i] ] ;
trace( formatter.format( "{0} scored {1,5}" ) );
}
// "A scored 16"
// "B scored 32"
// "C scored 128"
// "D scored 1024"
// resolve toString
var x:Object = {};
x.toString = function() { return "john doe"; } ;
formatter.parameters = [ x ] ;
trace( formatter.format( "Who is {0} ?" ) ) ; //"Who is john doe ?"
// you can off course reuse the index
formatter.parameters = ["apple", "banana", "pineapple"] ;
trace( formatter.format( "I like all fruits {0},{1},{2}, etc. but still I prefer above all {0}" ) ); // "I like all fruits apple,banana,pineapple, etc. but still I prefer above all apple"
// indexed from an array + the arguments
formatter.parameters = [ ["apple", "banana", "pineapple"] , "grape", "tomato" ];
trace( formatter.format( "fruits: {0}, {1}, {2}, {3}, {4}" ) ); //"fruits: apple, banana, pineapple, grape, tomato
// passing reference and padding
var what = "answer" ;
formatter.parameters = [ {answer:"my answer"} , what ] ;
trace( formatter.format( "your {0} is within {answer,20:.}" ) ) // "your answer is within ...........my answer"
// using evaluated parameters
formatter.parameters = null ;
formatter.evaluators = { math: new MathEvaluator() };
trace( formatter.format( "my result is ${2+3}math$" ) ) ; // "my result is 5"
//by default evaluated parameters use EdenEvaluator with serialized result
formatter.parameters = null ;
formatter.evaluators = null ;
trace( formatter.format( "here some object '${ {a:1,b:2} }$'" ) ) ; //"here some object '{a:1,b:2}'"
//use EdenEvaluator with stringified result
formatter.parameters = null ;
formatter.evaluators = { math: new EdenEvaluator(false) };
trace( formatter.format( "here some object '${ {a:1,b:2} }eden$'" ) ) ; //"here some object '[object Object]'"
trace( formatter.format( "the host is ${system.Environment.host}eden$" ) ); //"the host is Flash 9.0.115.0"
//use chained evaluators
formatter.parameters = null ;
formatter.evaluators = { eden: new EdenEvaluator(false), date: new DateEvaluator() ];
trace( formatter.format( "my date is ${new Date(2007,4,22,13,13,13)}eden,date$" ) ) ; //"my date is 22.05.2007 13:13:13"
| Property | Defined By | ||
|---|---|---|---|
| evaluators : Object
Contain a list of evaluators to be used in Strings.format
ex:
Strings.evaluators = { math: new MathEvaluator() };
Strings.format( "my result is ${2+3}math$" ); // "my result is 5"
note:
property names in the evaluators object can only contains
lower case alphabetical chars and digit chars
| StringFormatter | ||
| parameters : Array
The parameters used in the formatter. | StringFormatter | ||
| source : String
The source to format. | StringFormatter | ||
| Property | Defined By | ||
|---|---|---|---|
| output : String
The source to format. | StringFormatter | ||
| Method | Defined By | ||
|---|---|---|---|
StringFormatter(parameters:Array = null, source:String = null, evaluators:Object = null)
Creates a new StringFormatter instance. | StringFormatter | ||
format(value:* = null):String
Formats the specified value. | StringFormatter | ||
| evaluators | property |
public var evaluators:ObjectContain a list of evaluators to be used in Strings.format ex: Strings.evaluators = { math: new MathEvaluator() }; Strings.format( "my result is ${2+3}math$" ); // "my result is 5" note: property names in the evaluators object can only contains lower case alphabetical chars and digit chars
| output | property |
protected var output:StringThe source to format.
| parameters | property |
public var parameters:ArrayThe parameters used in the formatter.
| source | property |
public var source:StringThe source to format.
| StringFormatter | () | Constructor |
public function StringFormatter(parameters:Array = null, source:String = null, evaluators:Object = null)Creates a new StringFormatter instance.
Parametersparameters:Array (default = null) | |
source:String (default = null) | |
evaluators:Object (default = null) |
| format | () | method |
public function format(value:* = null):StringFormats the specified value.
Parameters
value:* (default = null) — The object to format.
|
String — the string representation of the formatted value.
|