Lua/Corona字符串文字 [英] Lua / Corona String Text

查看:100
本文介绍了Lua/Corona字符串文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用几个变量填充文本的命令是什么.

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

ex>天:%1是第%3周中的第%2天"

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

其中%1可能是5,%2可能是10,%3 12

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

在另一文本中,可能是: 例如,在%3周中,第%2天是您的一天:%1"

in another text it could be: ex> "in the %3 week the %2th day is your Day: %1"

在%1始终获得变量1,%2变量2和%3变量3

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.

谢谢 克里斯

推荐答案

Lua内置的用一些变量填充文本"功能是

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

您可以编写一个使用gsub查找%n的所有实例,抓住n的实例,然后按位置查找您的参数之一,从而按照自己的方式工作:

You can write one that works they way you want by using gsub to find all instances of %n, grab the n and 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                               

这篇关于Lua/Corona字符串文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆