How would I put “\” in a string without the apostrophe in the front of it being cancelled out?

Viewed 106

For example, if I were to do this:

    print(“\”)

It would say: `unfinished string near: ‘“”’ instead of my expected output of: ‘\’

How would I print this? I have searched on google yet still have yet to find an answer.

1 Answers

The backslash (\) is escaping the following character, being the double quote ("), causing the string to be unfinished.

To include an actual backslash in your string, you escape it with another backslash:

print("\\")

From Lua 5.4 Reference Manual, §3.1 (emphasis mine):

A short literal string 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]). [...]

Related