字符串没有附加运算符。字符串是不可变的值。
The ..
运算符连接两个字符串,生成第三个字符串:
local b = "con"
local c = "catenate"
local a = b .. c -- "concatenate"
The table.concat
函数连接表中的字符串,生成字符串结果:
local t = { "con", "catenate" }
local a = table.concat(t) -- "concatenate"
local t = { "two", "words" }
local a = table.concat(t, " ") -- "two words"
The string.format
函数采用带有兼容值列表的格式模式,生成字符串结果:
local b = 2
local c = "words"
local a = string.format("%i %s", b, c) -- "2 words"
local t = { 2, "words" }
local a = string.format("%i %s", unpack(t)) -- "2 words"
如果您正在积累大量最终想要连接的字符串,则可以使用表作为临时数据结构,并在完成积累后进行连接:
local t = {}
for i = 1, 1000 do
table.insert(t, tostring(i))
end
local a = table.concat(t) -- "1234...9991000"
对于大量字符串,您可以增量连接。看LTN 9:逐段创建字符串 http://www.lua.org/notes/ltn009.html以及相关讨论。