lang • subst


Result = Subst$ ( Pattern , ReplaceString [ , ReplaceString ... ] ) Result = Subst ( Pattern , ReplaceString [ , ReplaceString ... ] )

Replace substrings &1, &2, etc. in a pattern with the first, second, and subsequent ReplaceString argument respectively, and return the result.

If Pattern is null, then a null string is returned.

For C developers, this is not unlike a simplified sprintf.


Print Subst("Gambas is a cool &1", "BASIC")
Gambas is a cool BASIC

This function is very useful when you must concatenate strings that must be translated. Do not use the & operator, as the order of concatenation may change with the language.

For example:

Print Subst(("Today, we are &1 &2"), Format(Now, "mmm"), Format(Now, "d"))

will be translated in french this way:

Print Subst(("Aujourd'hui, nous sommes le &2 &1"), Format(Now, "mmm"), Format(Now, "d"))

In Gambas 3, if you want to substitute an argument whose index is greater or equal than ten, you have to enclose the argument index between { and }.

For example:

DIM aArg AS String[]
PRINT Subst("The 9th argument is &9 and the 10th is &{10}",
  aArg[1], aArg[2], aArg[3], aArg[4], aArg[5], aArg[6], aArg[7], aArg[8], aArg[9], aArg[10])

That syntax is not mandatory in Gambas 2, and it was an error. For example, with the '&10' substitution pattern, you cannot make the difference between "substitute the 10th argument" and "substitute the first argument and add a zero".

See also