Many applications need to be able to perform text substitution, for
example:
Your balance at <time> on <date> is <currencyvalue>.
Translation of a sentence or message from one language to another may
result in changes to the displayed parameter order. The example, the
Afrikaans translation of this sentence requires a different order:
Jou balans op <date> om <time> is <currencyvalue>.
The words
SUBSTITUTE and
REPLACES provide for this
requirements by defining a text substitution facility. For example,
we can provide an initial string in the form:
Your balance at %time% on %date% is %currencyvalue%.
The
%
is used as delimiters for the substitution name. The
text "
currencyvalue
", "
date
" and "
time
"
are text substitutions, where the replacement text is defined by
REPLACES:
The substitution name "date" is defined to be replaced with the string
"10/Nov/2014" and "time" to be replaced with "02:52". Thus
SUBSTITUTE would produce the string:
Your balance at 02:52 on 10/Nov/2014 is %currencyvalue%.
As the substitution name "currencyvalue" has not been defined, it is
left unchanged in the resulting string.
The return value
n is nonnegative on success and indicates the
number of substitutions made. In the above example, this would be two.
A negative value indicates that an error occurred.
As substitution is not recursive, the return value could be used to
provide a recursive substitution.
Implementation of
SUBSTITUTE may be considered as being equivalent
to a wordlist which is searched. If the substitution name is found, the
word is executed, returning a substitution string.
Such words can be deferred or multiple wordlists can be used.
The implementation techniques required are similar to those used by
ENVIRONMENT?.
There is no provision for changing the delimiter character, although a
system may provide system-specific extensions.