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

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.
主站蜘蛛池模板: 阿荣旗| 兰州市| 台安县| 旅游| 沁源县| 镇宁| 霍山县| 龙陵县| 名山县| 巫山县| 兰考县| 梅州市| 修水县| 松江区| 图木舒克市| 扶风县| 建宁县| 社会| 会同县| 霍林郭勒市| 英超| 珲春市| 喀什市| 红安县| 凌源市| 大安市| 福泉市| 黎川县| 洛南县| 漯河市| 蒙山县| 新安县| 北辰区| 凯里市| 永宁县| 镇赉县| 阿图什市| 樟树市| 栾川县| 明光市| 康马县|