Instant methods: format()
The instant method format() replaces in the string operand all placeholders by corresponding string operands provided in parameters of the method and returns the resulting string.
Declaration
string string.format( string aArg1 )
string string.format( string aArg1, string aArg2 )
string string.format( string aArg1, string aArg2, ..., string aArg9 )
Parameters
aArg1
The string to replace all occurrences of the placeholder {1} found in the string operand.
aArg2
The string to replace all occurrences of the placeholder {2} found in the string operand.
...
aArg9
The string to replace all occurrences of the placeholder {9} found in the string operand.
Discussion
The format() method evaluates the content of the string, in context of which the method has been called searching for placeholders {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8} and {9}. Each found placeholder is replaced by the string passed in the corresponding method parameter aArg1, aArg2, ... aArg9. The method returns the new string without modifying any of the involved string operands. For example:
var string msg = "Deleting file named {1} from totally {2} files."; var string op1 = "settings.bin"; var string op2 = "5"; var string result = msg.format( op1, op2 ); // result = "Deleting file named settings.bin from totally 5 files."
This method is convenient to format text messages containing dynamically estimated fragments, like files names, date, numbers, etc.. Please note, the method format() is limited to accept maximally 9 parameters. Missing parameters are considered as being empty strings. In the following example the method is invoked with two parameters only. The corresponding placeholder {3} is therefore replaced by an empty string:
var string msg = "{1} + {3} = {2}"; var string op1 = "13"; var string op2 = "69"; var string result = msg.format( op1, op2 ); // result = "13 + = 69"