有没有办法根据现有的'引用样式字符串转换或创建新的[[括号样式字符串]]?
s = "one\ntwo" -- how the string was created
s2 = [[one\ntwo]] -- what i want the new string to be
答案 0 :(得分:2)
逃避逃逸序列似乎达到了预期的效果,至少在这种情况下。
s2 = string.gsub(s, "\n", "\\n")
> print(s2)
one\ntwo
答案 1 :(得分:2)
一种方法是创建一个包含所有可能转义序列的表:
local t = {["\a"] = [[\a]],
["\b"] = [[\b]],
["\f"] = [[\f]],
["\n"] = [[\n]],
["\r"] = [[\r]],
["\t"] = [[\t]],
["\r"] = [[\r]],
["\\"] = [[\\]],
["\""] = [["]],
["\'"] = [[']],
}
local s2 = s:gsub(".", t)