A Fuse Powered Company

Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

LUA String Text !
Started by guruk Apr 18 2014 11:14 AM

- - - - -
4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

guruk

[GLOBAL: userInfoPane.html]
guruk
  • Pro
  • PipPipPipPipPipPip
  • 582 posts
  • Jedi

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 :)

 

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

possitions.

 

thanks

chris

 



[TOPIC: post.html]
#2

paulscottrobson

[GLOBAL: userInfoPane.html]
paulscottrobson
  • Starter
  • PipPipPipPipPipPip
  • 454 posts
  • Jedi

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 :)

 

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 %'.



[TOPIC: post.html]
#3

guruk

[GLOBAL: userInfoPane.html]
guruk
  • Pro
  • PipPipPipPipPipPip
  • 582 posts
  • Jedi

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                               


[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Corona Staff
  • 10,761 posts
  • Jedi

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



[TOPIC: post.html]
#5

paulscottrobson

[GLOBAL: userInfoPane.html]
paulscottrobson
  • Starter
  • PipPipPipPipPipPip
  • 454 posts
  • Jedi

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 ?




[topic_controls]
[/topic_controls]