lua-users home
lua-l archive

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

It was thus said that the Great Soni L. once stated:
> In Lua, you can't put a line break in the middle of a string. However, 
> ever since Lua 5.2, newlines are equivalent to whitespace everywhere else.
> $ lua
> Lua 5.3.3  Copyright (C) 1994-2016, PUC-Rio
> > "test
> >> test"
> stdin:1: unfinished string near '"test'
> vs
> $ lua
> Lua 5.3.3  Copyright (C) 1994-2016, PUC-Rio
> > do
> >> print("x")
> >> (print)("test")
> >> end
> x
> stdin:2: attempt to call a nil value
> stack traceback:
>     stdin:2: in main chunk
>     [C]: in ?
> Note that I'm asking for multiline strings that support escapes, not the 
> removal of raw strings.
> (I'm fine with line comments the way they are, tho)

  Section 3.1 of the Lua 5.3 manual:

	Literal strings can be delimited by matching single or double
	quotes, and can contain the following C-like escape sequences: '\a'
	(bell), '\b' (backspace), '\f' (form feed), '\n' (newline), '\r'
	(carriage return), '\t' (horizontal tab), '\v' (vertical tab), '\\'
	(backslash), '\"' (quotation mark [double quote]), and '\''
	(apostrophe [single quote]). A backslash followed by a real newline
	results in a newline in the string. The escape sequence '\z' skips
	the following span of white-space characters, including line breaks;
	it is particularly useful to break and indent a long literal string
	into multiple lines without adding the newlines and spaces into the
	string contents.

So, one could do:

test = "Hi.  I'm Soni, \
The manual does indeed answer this question.\
Is that not nifty?"


The '\z' escape sequence also works back to Lua 5.2.  You might then want to
experiment with various forms of '\<literal newline>' and '\z' to see the
effect they have on strings.