Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
20

What's the difference between ' and "? [closed]

Asked by 9 years ago

I'm pretty sure they work the same, but have different meanings.

print ('Hello World')

vs

print ("Hello World")

7
Strings that use ""s can use ''s inside them without ending the original string. SanityMan 239 — 9y

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?

2 answers

Log in to vote
32
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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.

  • It can contain newlines explicitly (span multiple lines and it will include the appropriate \n or \r\n characters
  • It cannot contain escape sequences: [[\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.

17
Good explanation, really detailed. Upvoted. Lacryma 548 — 9y
1
These really are the kinds of answers that you can get tons of points from. Cheers, BlueTaslem! yumtaste 476 — 9y
Ad
Log in to vote
8
Answered by
Lacryma 548 Moderation Voter
9 years ago

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.