问题
So I started to learn Lua(5.1) and I saw that thing called literal strings. And I have no idea what these do. The manual says \a is a bell but when I type
print('hello\athere')
The IDE prints a weird square with 'bel' written on it. So if someone could help me and explain every one of them[Literal Strings]. that would be really helpful.
p.s. i use Sublime Text 3
回答1:
Only ASCII between 0x20
and 0x7E
are printable characters. How other characters are output, including '\a'
and '\b'
, is up to the implementation.
'\a'
, the ASCII 7
for BEL
, is designed to be used to alert. Typical terminal would make an audible or visible alert when outputing '\a'
. Your IDE choose to show a different output other than an alert. That's OK since it's up to the implementation.
回答2:
A literal is not more than a value inside the code, e.g.: 'some text'
.
The '\a'
is something different. A special "char", that is used to output a sound (was using the pc-speaker some aeons ago).
回答3:
Such sequences are called "escape sequences", and are found in many different languages. They are used to encode non-printable characters such as newlines in literal (hardcoded) strings.
Lua supports the following escape sequences:
\a
: Bell\b
: Backspace\f
: Form feed\n
: Newline\r
: Carriage return\t
: Tab\v
: Vertical tab\\
: Backslash\"
: Double quote\'
: Single quote\nnn
: Octal value (nnn
is 3 octal digits)\xNN
: Hex value (Lua5.2/LuaJIT,NN
is two hex digits)
来源:https://stackoverflow.com/questions/26803512/literal-strings-lua-5-1