我正在尝试将一段联系的字符串从Lua输出到控制台。当字符串显示在控制台中时,它会自动在其前面和后面加上双引号。我想在字符串中间有一些其他双引号,但我不能这样做。
我已经尝试了几种不同的方法,如下面的评论所示,但这些方法都不起作用。输出通常如下所示:
1) "10000": "1543412332"
2) "10001": "1543233731"
3) "10003": "1543637245"
4) "10004": "1543227124"
5) "10005": "1543226828"
但我希望输出是:
1) "10000": "1543412332"
2) "10001": "1543233731"
3) "10003": "1543637245"
4) "10004": "1543227124"
5) "10005": "1543226828"
这是我的代码
for index = 1, table.maxn(resultKey) do
local unconcatted = {[1] = resultKey[index], [2] = [[": "]], [3] = resultValue[index]}
-- local unconcatted = {[1] = """, [2] = resultKey[index], [3] = "": "", [4] = resultValue[index], [5] = """}
-- local unconcatted = {[1] = resultKey[index], [2] = "": "", [3] = resultValue[index]}
-- local unconcatted = {[1] = resultKey[index], [2] = '": "', [3] = resultValue[index]}
local concatted = table.concat(unconcatted);
table.insert(resultFinal, 1, concatted);
end
return resultFinal;
要转义一种引号,请使用另一种引号!
"'" = single quote literal
'"' = double quote literal
[['"']] = string within [[]] is parsed as is (dropping leading newlines if there are any)
[=[I have [[ and ]] inside!]=] = Use `=` when you need [[]] inside, add more `=` if required
在您的情况下
local unconcatted = {'"', resultKey[index], '": "', resultValue[index], '"'}
应该做这项工作。如果您愿意,可以将'
对替换为[[
和]]
。
另请注意,我已经删除了索引。当您只需要一个数组并执行与以前相同的操作时,它是首选表示法,从而使您不必对更改进行计数并可能提高性能。
你的for
循环不是很好。 table.maxn
在 Lua 5.1 中已弃用,并在更高版本中删除。您应该使用新的语法来表示长度:
for index = 1, #resultKey do
end
或使用ipairs
:
for index,key in ipairs(resultKey) do
local unconcatted = {'"', key, '": "', resultValue[index], '"'}
…
end