Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Lua/Corona字符串文本_String_Lua_Coronasdk - Fatal编程技术网

String Lua/Corona字符串文本

String Lua/Corona字符串文本,string,lua,coronasdk,String,Lua,Coronasdk,用几个变量填充文本的命令是什么 日期:%1是第%3周中的第%2天 其中%1可以是5,%2可以是10,%3可以是12 在另一个文本中,可以是: ex>在%3周内,第%2天是您的一天:%1 %1始终获取变量1、%2变量2和%3变量3 我希望我可以理解: 这与我的语言文件和语法有关,变量有时会位于不同的位置 占有 谢谢 chrisLua内置的用几个变量填充文本函数是,它的工作原理与C类似 通过使用gsub查找%n的所有实例,获取该n并使用该n按位置查找一个参数,您可以编写一个按您希望的方式工作的参数:

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

日期:%1是第%3周中的第%2天

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

在另一个文本中,可以是: ex>在%3周内,第%2天是您的一天:%1

%1始终获取变量1、%2变量2和%3变量3

我希望我可以理解:

这与我的语言文件和语法有关,变量有时会位于不同的位置 占有

谢谢
chris

Lua内置的用几个变量填充文本函数是,它的工作原理与C类似

通过使用gsub查找%n的所有实例,获取该n并使用该n按位置查找一个参数,您可以编写一个按您希望的方式工作的参数:

function format(fmt, ...)
    local args = {...}
    return (fmt:gsub('%%(%d)', function(i) return args[tonumber(i)] end))
end
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                               
现在,您可以让占位符按位置引用参数:

function format(fmt, ...)
    local args = {...}
    return (fmt:gsub('%%(%d)', function(i) return args[tonumber(i)] end))
end
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                               

如果您愿意使用占位符名称而不是数字,您可以这样做:

message = "Day: %(day) is the %(weekday)th day in the %(week)th week"
print( message:gsub("%%%((%l+)%)", {day='monday', weekday=1, week=5}) )
您可以使用任何您想要的模式,就像您可以使用{}而不是仅仅相应地更改模式字符串一样。您也可以使用数字,但由于键必须是字符串,因此表格编写起来就不那么容易了,因此您需要繁琐的括号:

message = "Day: %1 is the %2th day in the %3th week"
print( message:gsub("%%(%d+)", {['1']='monday', ['2']=1, ['3']=5})  )
您可以定义一个函数,该函数添加一个将字符串键转换为数字的元表,然后可以完全删除该键,如下所示:

function str2numKeys(t)
    local mt = {}
    setmetatable(t, mt)
    mt.__index = function(t, k, v) return t[tonumber(k)] end
    return t
end

message = "Day: %1 is the %2th day in the %3th week"
print( message:gsub("%%(%d+)", str2numKeys {'monday', 1, 5})  )
但在这种情况下,你最好隐藏这些细节

function string:gsubNum(actuals)  
    local function str2numKeys(t)
        local mt = {}
        setmetatable(t, mt)
        mt.__index = function(t, k, v) return t[tonumber(k)] end
        return t
    end

    return self:gsub("%%(%d+)", str2numKeys(actuals))
end

message = "Day: %1 is the %2th day in the %3th week"
print( message:gsubNum {'monday', 1, 5}  )
message = "in the %3 week the %2th day is your Day: %1"
print( message:gsubNum {'monday', 1, 5}  )