Lua:转义字符串并写入文件

时间:2015-12-10 17:57:37

标签: lua

我创建函数writefile:

 function writefile(output,msgid)       
    file = io.open(output, "a")
    if (file) then                  
        file:write('msgid "' .. msgid .. '"')       
        file:close()
    else
        print("can't open file")
    end
end

参数是:

output = output.txt
msgid =  "We can overcome anything!"

我想在文件中输出msgid "\"We can overcome anything!\""

我不想要msgid = ""We can overcome anything!""

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果您打算根据Lua的转义规则转义字符串,那么您应该使用string.format's special %q formatting code

file:write(string.format("msgid "%q"", msgid))

答案 1 :(得分:0)

试试这个......

.extra(select={'color': "'blue'"})

我在这里做的是使用\来逃避另一个。我们不能直接把它放在那里,因为在Lua它是一个逃避角色。

相关问题