官术网_书友最值得收藏!

Escape characters

Strings need to be within quotes, but what happens when you need to put quotes inside the string? Lua doesn't care if a string uses single or double quotes, so long as the symbol at the start and end of the string matches, so technically this code would be valid:

message = 'he said "bye" and left'
print (message)

However, this is not desirable. As a convention, only double quotes will be used to represent a string throughout this book. To include a double quote within a string, the character must be escaped. Escaping a character means the character will be treated as part of the string, rather than a Lua instruction. To escape a character, place a \ in front of it, like so:

message = "he said \"bye\" and left"
print (message)

There are actually several escape characters that can be used when working with strings. The most often used escape characters are:

  • \n: Newline, moves the cursor down one line
  • \t: Horizontal tab, tabs over on the current line
  • \\: Backslash, you have to escape the escape symbol
  • \": Double quote, needed to include a quote in a string

The full list of supported escape symbols for Lua can be found online at  https://www.lua.org/pil/2.4.html.
主站蜘蛛池模板: 定安县| 华宁县| 阜康市| 新郑市| 永平县| 资阳市| 金川县| 张家界市| 江门市| 沅陵县| 内乡县| 永安市| 永泰县| 安徽省| 营口市| 论坛| 云林县| 田林县| 遂平县| 乌审旗| 南溪县| 都昌县| 门头沟区| 黄平县| 定边县| 长岭县| 元谋县| 广宗县| 兖州市| 茶陵县| 南和县| 灵璧县| 濉溪县| 五华县| 开阳县| 册亨县| 临泽县| 绥化市| 河南省| 信阳市| 繁昌县|