Extracted at 2014-06-26.
pike.ida.liu.se
[Top]
Tools
Tools.String
Tools.String.NamedSprintf

Method Tools.String.NamedSprintf()->named_sprintf()


Method named_sprintf

string named_sprintf(string format, mapping data)

Description

formats a string using named parameters, in the format of %{param} where "param" is a key in the data set being used for replacements. Additionally, if a parameter contains a colon (:), anything following the colon will be considered a formatting option to sprintf for the data. If no formatting options are specified, the data will be cast to a string and be inserted into the final string as if %s were passed in sprintf (). If formatting options are specified, the data will be passed as-is.

Parameter format

format string, containing replacement fields in the form of %{param}

Parameter data

a mapping with data to be replaced

Example

> Tools.String.named_sprintf("Hello, %{name}. Lunch will be $%{cost:.2f}.", (["name": "James", "planet": "Mars", "cost": 2.5])); (2) Result: "Hello, James. Lunch will be $2.50."