FormatMessage - Maple Help

# Online Help

###### All Products    Maple    MapleSim

StringTools

 FormatMessage
 format a message string using %-interpolation

 Calling Sequence FormatMessage( fmt, param1, ..., param9 )

Parameters

 fmt - string param1, ..., param9 - expressions

Description

 • The FormatMessage procedure is used to interpolate %-specifiers in format strings. These are used in exceptions and warning messages throughout Maple.
 • The FormatMessage(fmt, param1, param2, ...) function produces an output string from the format string fmt according to the following rule.
 The string fmt is copied to the output and, whenever a '%' character followed by a decimal digit but not immediately preceded by another '%' character appears, it, and the adjacent digit, are replaced in the output by the result of converting the d-th parameter to a string. Here, d is the decimal value of the digit following the '%' character.
 An exception to this rule is when the digit has the value $d=0$. In this case, all of the remaining parameters are copied to the output as a (comma,space)-separated sequence. If the '%' character is followed immediately by a '-' character, which is, in turn, followed immediately by a non-zero decimal digit with decimal value d, then the d-th parameter paramd is taken to be a small non-negative integer. The number is copied to the output as an ordinal of the same value.
 If converting any of the arguments paramd results in a very long string, the string will be cut off. One consequence is that this command should not be used to generate strings that will be used as executable Maple code from arbitrary expressions. It is best to use sprintf (with the '%a' format specifier) for such cases.
 • Up to nine parameters parami can appear after the format string fmt in the calling sequence.
 • The FormatMessage procedure also supports automated pluralization. If, instead of a digit, a '%' character is followed by an expression in braces of the form %{d|<0 value>|<1 value>|}, then the d-th parameter, which must be a non-negative integer, is replaced conditionally by whichever of the value tokens matches in number. Specifically, if the d-th parameter is 0, then the <0 value> string is interpolated; if the d-th parameter is 1, then the <1 value> string is interpolated; and, if the d-th parameter is an integer greater than 1, then the  string is interpolated.
 • This function is part of the StringTools package, and so it can be used in the form FormatMessage(..) only after executing the command with(StringTools). However, it can always be accessed through the long form of the command by using the form StringTools[FormatMessage](..).
 Note: The output of FormatMessage should be used for showing a message to a human, not for formatting Maple objects for parsing by Maple or other programs. For more sophisticated formatting capabilities, see sprintf.
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings.

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{FormatMessage}\left("foo"\right)$
 ${"foo"}$ (1)
 > $\mathrm{FormatMessage}\left("foo","bar",2,3\right)$
 ${"foo"}$ (2)
 > $\mathrm{FormatMessage}\left("foo %1","bar",2,3\right)$
 ${"foo bar"}$ (3)
 > $\mathrm{FormatMessage}\left("foo %2","bar",2,3\right)$
 ${"foo 2"}$ (4)
 > $\mathrm{FormatMessage}\left("foo %3","bar",2,3\right)$
 ${"foo 3"}$ (5)
 > $\mathrm{FormatMessage}\left("foo %0","bar",2,3\right)$
 ${"foo bar, 2, 3"}$ (6)
 > $\mathrm{FormatMessage}\left("foo %-2","bar",2,3\right)$
 ${"foo 2nd"}$ (7)
 > $\mathrm{FormatMessage}\left("foo %-3","bar",2,3\right)$
 ${"foo 3rd"}$ (8)
 > $\mathrm{FormatMessage}\left("There %\left\{1|are|is|are\right\} %\left\{1|no|one|many\right\} thing%\left\{1|s||s\right\}.",0\right)$
 ${"There are no things."}$ (9)
 > $\mathrm{FormatMessage}\left("There %\left\{1|are|is|are\right\} %\left\{1|no|one|many\right\} thing%\left\{1|s||s\right\}.",1\right)$
 ${"There is one thing."}$ (10)
 > $\mathrm{FormatMessage}\left("There %\left\{1|are|is|are\right\} %\left\{1|no|one|many\right\} thing%\left\{1|s||s\right\}.",5\right)$
 ${"There are many things."}$ (11)
 > $\mathrm{FormatMessage}\left("foo %-1","N",2,3\right)$
 ${"foo N-th"}$ (12)
 > $\mathrm{fopen}\left(2\right)$
 > $\mathrm{FormatMessage}\left(\mathrm{lastexception}\left[2..-1\right]\right)$
 ${"invalid input: fopen expects its 1st argument, file, to be of type \left\{string, symbol\right\}, but received 2"}$ (13)
 > $\mathrm{lastexception}$
 ${0}{,}{"invalid input: %1 expects its %-2 argument, %3, to be of type %4, but received %5"}{,}{\mathrm{fopen}}{,}{1}{,}{\mathrm{file}}{,}\left\{{\mathrm{string}}{,}{\mathrm{symbol}}\right\}{,}{2}$ (14)

Compatibility

 • The StringTools[FormatMessage] command was updated in Maple 15.

 See Also