lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

> What's the easiest way to write a "raw" string out [?]

  You can use the option "%q" in "format". For instance:

aString = "Hello,\nHow are you?\n"
print(format('%q', aString))

Results in:

How are you?\

Notice that '%q' puts the quotes back in the string...

  Another option is to use "gsub", but just once:

       gsub(aString, "([\n\t\"])", "\\%1")

  The |write("[[",s,"]]")| form is not 100% safe, since the string *may have*
an unbalanced "[[" or "]]".

-- Roberto

PS: from the Lua manual (3.0), section 4.1:

" Literal strings can also be delimited by matching [[ ... ]]. Literals in
this bracketed form may run for several lines, may contain nested [[ ...  ]]
pairs, and do not interpret escape sequences. This form is specially
convenient for handling strings that contain program pieces or other
quoted strings. "