LUA String Text !

Hi,

what would be the command to fill a text with a few variables.

ex>  “Day: %1 is the %2th day in the %3th week”

where %1 could be 5 , %2 could be 10 and %3 12

in another text it could be:

ex>  “in the %3 week the %2th day is your Day: %1”

while always the %1 get variable 1, %2 variable  2 and %3 variable 3

i hope i made it understandable :slight_smile:

its about my Language File and about grammer the variables would sit sometimes at different

possitions.

thanks

chris

Use search and replace string:gsub()

s1 = "Day %1 is %2" s1 = s1:gsub("%%1","17"):gsub("%%2","Monday") print(s1)

Should print Day 17 is Monday. He said hopefully :slight_smile:

The double % is because lua uses a quasi regular expression system, and % is used for other things (e.g. %w matches a word), %% means ‘this % actually IS a %’.

thanks for ur effort

finally best answer was on stackoverflow.com:

 

Lua’s built-in “fill a text with a few variables” function is string.format, which works like C’s printf family of functions.

You can write one that works they way you want by using gsub to find all instances of %n, grab the nand use that to look up one of your arguments by position:

function format(fmt, …)

    local args = {…}

    return (fmt:gsub(’%%(%d)’, function(i) return args[tonumber(i)] end))

end

Now you can have your place holders refer to arguments by position:

format(“Day: %1 is the %2th day in the %3th week”, day, weekday, week)     --> Day: 22 is the 2th day in the 3th week    

format(“in the %3 week the %2th day is your Day: %1”, day, weekday, week) --> in the 3 week the 2th day is your Day: 22 

format("%1 %2 %3 %2 %1", day, weekday, week)                               --> 22 2 3 2 22                               

There is a function called string.format() which is what you’re looking for:

local someString = string.format(“This is %s and I have %d computers”, “Rob”, 3)

Rob

I think the OP wanted to change the order of the substitution parameters depending on internationalisation. I thought string:format() worked like sprintf() rather than Python’s format ?

Use search and replace string:gsub()

s1 = "Day %1 is %2" s1 = s1:gsub("%%1","17"):gsub("%%2","Monday") print(s1)

Should print Day 17 is Monday. He said hopefully :slight_smile:

The double % is because lua uses a quasi regular expression system, and % is used for other things (e.g. %w matches a word), %% means ‘this % actually IS a %’.

thanks for ur effort

finally best answer was on stackoverflow.com:

 

Lua’s built-in “fill a text with a few variables” function is string.format, which works like C’s printf family of functions.

You can write one that works they way you want by using gsub to find all instances of %n, grab the nand use that to look up one of your arguments by position:

function format(fmt, …)

    local args = {…}

    return (fmt:gsub(’%%(%d)’, function(i) return args[tonumber(i)] end))

end

Now you can have your place holders refer to arguments by position:

format(“Day: %1 is the %2th day in the %3th week”, day, weekday, week)     --> Day: 22 is the 2th day in the 3th week    

format(“in the %3 week the %2th day is your Day: %1”, day, weekday, week) --> in the 3 week the 2th day is your Day: 22 

format("%1 %2 %3 %2 %1", day, weekday, week)                               --> 22 2 3 2 22                               

There is a function called string.format() which is what you’re looking for:

local someString = string.format(“This is %s and I have %d computers”, “Rob”, 3)

Rob

I think the OP wanted to change the order of the substitution parameters depending on internationalisation. I thought string:format() worked like sprintf() rather than Python’s format ?