Home  Contents

string.arg

String Core4 Lua Commands

SYNOPSIS

result = string.arg(text[, arg[, ...]])

DESCRIPTION

This function will replace all occurrences of %1 in text with the first arg.
Consecutively, all occurrences of %2 will be replaced by the second arg and so on.
Up to ten different replacements are possible, with %0 being used for the tenth value.

This is useful for multilingual applications that have to insert a runtime generated value into a string. Different languages often have a different idea of the order in which text must be inserted.

All occurrences of %x that are higher than the number of arg parameters given are renumbered down. For example, if there are three arg values, %4 would become %1, %5 would become %2, etc... This will cause cascaded use of this function do the right thing.

All arg values are processed in string context, any arg that is not a string will have a tostring() call thrown at it first before being added to the result.

RETURN VALUE

A string with all possible replacements done.

EXAMPLE

This example shows how a date can be formatted properly for the US American or German locale.

print(string.arg("Today is %2/%3/%1", 1999,12,31))
Today is 12/31/1999
print(string.arg("Heute ist der %3.%2.%1", 1999,12,31))
Heute ist der 31.12.1999

An example that shows the renumbering of unused positions.

print(string.arg("%1 %2 %3 %5 %4", "A", "B", "C"))
A B C %2 %1
>  >  > 
str = string.arg("%1 %2 %3 %5 %4", "A", "B", "C"); str = string.arg(str, "X", "U") print(str)
A B C U X

SEE ALSO