Packagesystem
Classpublic class Strings
InheritanceStrings Inheritance Object

A static class for String utilities.



Public Properties
 PropertyDefined By
  evaluators : Object
[static] 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
Strings
Public Methods
 MethodDefined By
  
compare(o1:String, o2:String, strict:Boolean = false):int
[static] Compares the two specified String objects.
Strings
  
endsWith(str:String, value:String):Boolean
[static] Determines whether the end of this instance matches the specified String.
Strings
  
format(format:String, ... args):String
[static] Format a string using indexed, named and/or evaluated parameters.
Strings
  
splitToChars(str:String, modifier:String = toString):Array
[static] Split every individual char in a string to an array of Char instances.
Strings
  
startsWith(str:String, value:String):Boolean
[static] Determines whether a specified string is a prefix of the current instance.
Strings
Property Detail
evaluatorsproperty
public static var 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

Method Detail
compare()method
public static function compare(o1:String, o2:String, strict:Boolean = false):int

Compares the two specified String objects. Allows to take into account the string case for comparison.

Parameters

o1:String — first string to compare with the second string.
 
o2:String — second string to compare with the first string.
 
strict:Boolean (default = false) — (optionnal) useCase boolean, default to false.

Returns
int
endsWith()method 
public static function endsWith(str:String, value:String):Boolean

Determines whether the end of this instance matches the specified String.

Example :

         import system.Strings ;
         
         var result:;
         
         result = Strings.endsWith( "hello world", "world" );
         trace("> " + result) ; // true
         
         result = Strings.endsWith( "hello world", "hello" );
         trace("> " + result) ; // false
         

Parameters

str:String
 
value:String

Returns
Boolean
format()method 
public static function format(format:String, ... args):String

Format a string using indexed, named and/or evaluated parameters.

Method call :

  • StringUtil.format(pattern:String, ...arguments:Array):String
  • StringUtil.format(pattern:String, [arg0:arg1:arg2: ...] ):String
  • StringUtil.format(pattern:String, [arg0:arg1:arg2: ...], ...args:Array ):String
  • StringUtil.format(pattern:String, {name0:value0,name1:value1,name2:value2, ...} ):String
  • StringUtil.format(pattern:String, {name0:value0,name1:value1,name2:value2, ...} , ...args:Array ) :String
  • 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 "}}"

  • "some {{formatitem}} to be escaped" -> "some {formatitem} to be escaped"
  • "some {{format {0} item}} to be escaped", "my" -> "some {format my titem} to be escaped"
  • Example :

             import system.Strings ;
             
             var result:String ;
             
             // indexed from the arguments
             
             result = Strings.format( "hello {1} {0} world", "big", "the" ); 
             trace( result ) ; //"hello the big world"
             
             result = Strings.format("Brad's dog has {0,6:#} fleas.", 41) ;
             trace( result ) ;
             
             result = Strings.format("Brad's dog has {0,-6} fleas.", 12) ;
             trace( result) ;
             
             result = Strings.format("{3} {2} {1} {0}", "a", "b", "c", "d") ;
             trace( result ) ;
             
             // named from an object
             
             result = Strings.format( "hello I'm {name}", {name:"HAL"} );
             trace( result ) ; //"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<names.length; i++ )
             {
                 trace( Strings.format( "{0} scored {1,5}", [names[i], scores[i]] ) );
             }
             // "A scored    16"
             // "B scored    32"
             // "C scored   128"
             // "D scored  1024"
             
             // resolve toString
             var x:Object = {};
             x.toString = function() { return "john doe"; } ;
             trace( Strings.format( "Who is {0} ?", x ) ) ; //"Who is john doe ?"
             
             // you can off course reuse the index
             var fruits:Array = ["apple", "banana", "pineapple"] ;
             trace( Strings.format( "I like all fruits {0},{1},{2}, etc. but still I prefer above all {0}", fruits ) ); // "I like all fruits apple,banana,pineapple, etc. but still I prefer above all apple"
             
             // indexed from an array + the arguments
             var fruits:Array = ["apple", "banana", "pineapple"];
             trace( Strings.format( "fruits: {0}, {1}, {2}, {3}, {4}, {5}", fruits, "grape", "tomato" ) ); //"fruits: apple, banana, pineapple, grape, tomato, undefined"
             
             // passing reference and padding
             var what = "answer" ;
             result = Strings.format( "your {0} is within {answer,20:.}", {answer:"my answer"}, what ) ; 
             trace( result ) // "your answer is within ...........my answer"
             
             // using evaluated parameters
             Strings.evaluators = { math: new MathEvaluator() };
             Strings.format( "my result is ${2+3}math$" ); // "my result is 5"
             
             //by default evaluated parameters use EdenEvaluator with serialized result
             Strings.format( "here some object '${ {a:1,b:2} }$'" ); //"here some object '{a:1,b:2}'"
             
             //use EdenEvaluator with stringified result
             Strings.evaluators = { math: new EdenEvaluator(false) };
             Strings.format( "here some object '${ {a:1,b:2} }eden$'" ); //"here some object '[object Object]'"
             Strings.format( "the host is ${system.Environment.host}eden$" ); //"the host is Flash 9.0.115.0"
             
             //use chained evaluators
             Strings.evaluators = { eden: new EdenEvaluator(false), date: new DateEvaluator() ];
             Strings.format( "my date is ${new Date(2007,4,22,13,13,13)}eden,date$" ); //"my date is 22.05.2007 13:13:13"
             
             

    Parameters

    format:String
     
    ... args

    Returns
    String
    splitToChars()method 
    public static function splitToChars(str:String, modifier:String = toString):Array

    Split every individual char in a string to an array of Char instances.

    Parameters

    str:String — The string to split
     
    modifier:String (default = toString) — The optional string modifier name (a function to apply)

    Returns
    Array
    startsWith()method 
    public static function startsWith(str:String, value:String):Boolean

    Determines whether a specified string is a prefix of the current instance.

    Example :

             import system.Strings ;
             
             trace( Strings.startsWith("hello world", "h") ) ; // true
             trace( Strings.startsWith("hello world", "hello") ) ; // true
             trace( Strings.startsWith("hello world", "a") ) ; // false
             

    Parameters

    str:String
     
    value:String

    Returns
    Booleantrue if the specified string is a prefix of the current instance.