I'm pretty sure they work the same, but have different meanings.
print ('Hello World')
vs
print ("Hello World")
There is no real difference between '
and "
. Their technical purpose is delimiting string literals that can have escape sequences.
They obviously must be paired, you cannot use "String'
or 'String"
. Because of this, if you need to include single quotes in a string, it may be more convenient to use double quotes: "a'l'o't'o'f'qu'o't'e's"
or vice versa: 'a"l"o"t"o"f"qu"o"t"e"s'
.
However, it's usually considered best style to just use one of the two through a script. Because it is the standard in most other languages, the one picked is usually double quotes. Your preference can override this.
If you do need a double quote inside of your string, you can escape it using the backslash: "He said, \"Wow!\""
.
Other characters are also escaped, e.g., the backslash: #"\\"
is one, the \
character.
There's also a few like " \a"
or "\b"
which correspond to control characters (see table here).
Finally, you can escape the byte value of a number: "\65"
is "A"
.
There is a third type of string literal using double square brackets: [[this is a string]]
.
This literal is different from the other two.
[[\65]]
is just like the string "\\65"
.One small detail is that if you start a double-square literal with a new line, e.g.
text = [[ hello there!]]
It will ignore the first newline, hence, text:sub(1,1) == 'h'
.
EDIT: There is a fourth type of string literal. [=[this is a string]=]
. This type is very similar to [[ strings ]]
. The difference is subtle but very useful:
cat = [[ this text can't contain [[ double squared text ]] the brackets broke the string! ]]
This first ]]
ends the string, because the second [[
is ignored as just part of the text contents.
That is not the case when dealing with [=[
:
cat = [=[ this text CAN contain [=[ these weird pairings ]=] -- (Ignore the syntax highlighter on ScriptingHelpers, it isn't correct) ]=]
because it keeps track of how many times it has opened, even within the insides. This is particularly useful for comments, since you can comment code including [[ ]]
strings
This is most useful for commenting out blocks of code that may contain block comments.
In other languages you will notice that
' is used to represent one character while
" is used to represent a string of characters.
In Lua, both ' and " are considered strings.
Locked by Shawnyg, UserOnly20Characters, and YellowoTide
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?