Namespace: Strings:format

Strings:format

Source:

Methods

format(template, replacements) → {String}

This is a pythonesque string format implementation. Apply formatted values to a string template, in which replacements are marked with curly braces.

Display literal curly brace with {{ and }}.

Unknown keys/indexes will be ignored.

This solution is adapted from: https://github.com/davidchambers/string-format

Parameters:
Name Type Description
template String
replacements Array.<String> | Object

arguments to insert into template, either as a dictionary, an array or a parameter sequence

Source:
Throws:

general exception on syntax errors

Returns:
the formatted string
Type
String
Example
format('An elephant is {times:float(0.00)} times smarter than a {animal}', {times : 5.5555, animal : 'lion'})
=> 'An elephant is 5.56 times smarter than a lion'
format('{0}{0}{0} ... {{BATMAN!}}', 'Nana')
=> 'NanaNanaNana ... {BATMAN!}'
format('{} {} {} starts the alphabet.', 'A', 'B', 'C')
=> 'A B C starts the alphabet.'
format('{0:int}, {1:int}, {2:int}: details are for wankers', '1a', 2.222, 3)
=> '1, 2, 3: details are for wankers'