function sap/base/strings/formatMessage

Visiblity: public
Available since: N/A
Module: sap/base/strings/formatMessage
Application Component:

Creates a string from a pattern by replacing placeholders with concrete values.

The syntax of the pattern is inspired by (but not fully equivalent to) the java.util.MessageFormat.

Placeholders have the form { integer }, where any occurrence of {0} is replaced by the value with index 0 in aValues, {1} by the value with index 1 in aValues etc.

To avoid interpretation of curly braces as placeholders, any non-placeholder fragment of the pattern can be enclosed in single quotes. The surrounding single quotes will be omitted from the result. Single quotes that are not meant to escape a fragment and that should appear in the result, need to be doubled. In the result, only a single single quote will occur.

Example: Pattern Strings

 formatMessage("Say {0}",     ["Hello"]) -> "Say Hello"    // normal use case
 formatMessage("Say '{0}'",   ["Hello"]) -> "Say {0}"      // escaped placeholder
 formatMessage("Say ''{0}''", ["Hello"]) -> "Say 'Hello'"  // doubled single quote
 formatMessage("Say '{0}'''", ["Hello"]) -> "Say {0}'"     // doubled single quote in quoted fragment
In contrast to java.util.MessageFormat, format types or format styles are not supported. Everything after the argument index and up to the first closing curly brace is ignored. Nested placeholders (as supported by java.lang.MessageFormat for the format type choice) are not ignored but reported as a parse error.

This method throws an Error when the pattern syntax is not fulfilled (e.g. unbalanced curly braces, nested placeholders or a non-numerical argument index).

This method can also be used as a formatter within a binding. The first part of a composite binding will be used as pattern, the following parts as aValues. If there is only one value and this value is an array it will be handled like the default described above.


Parameters Overview

Param Type Default Value Description
sPattern string A pattern string in the described syntax
aValues? any[] [] The values to be used instead of the placeholders.

Methods Overview

Method Description