Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What's the difference between ' and "?

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

print ('Hello World')

vs

print ("Hello World")

1 comment

7

Strings that use ""s can use ''s inside them without ending the original string.

2 answers

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.

2 comments

17

Good explanation, really detailed. Upvoted.

1

These really are the kinds of answers that you can get tons of points from. Cheers, BlueTaslem!

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.