You can use String.format in OCL expressions to build text from a template and one or more replacement values.
Syntax
String.format('template {0} {1}', value0, value1)
String.format is a static method on the String type. The first argument is the template string. Each following argument supplies a value for a placeholder in that template.
| Part | Meaning |
|---|---|
'template {0} {1}'
|
The text to produce. Placeholders in braces mark where values are inserted. |
{0}
|
The first replacement value after the template. |
{1}
|
The second replacement value after the template. |
{2}
|
The third replacement value after the template. |
Format text with replacement values
Use numbered placeholders to insert values into a message. Placeholder numbers are zero-based: {0} refers to the first value after the template, {1} to the second, and so on.
String.format('thetemplate {0} - {1} that can have any number of replacers, in this case {2}', 'hello', 'there', 3)
The expression produces:
thetemplate hello - there that can have any number of replacers, in this case 3
For example, use the same value more than once by repeating its placeholder:
String.format('Value: {0}; repeated value: {0}', 'hello')
This produces:
Value: hello; repeated value: hello
Match each placeholder to an argument
Provide an argument for every placeholder that the template uses. In the following example, the template uses {0}, {1}, and {2}, so it requires three replacement values after the template.
String.format('{0} - {1} - {2}', 'first', 'second', 'third')
This produces first - second - third.
Keep the placeholder number aligned with the intended argument. For example, {1} selects the second argument, not the first.
Dates and times
Use String.format to place a value in surrounding text. When you need a specific date/time representation, format the DateTime value with formatDateTime before including it in the message.
