What are the differences between ("") and ' '?

Not sure if this is the correct category, but I believe it is.

Since I have started scripting I have always used (“text”) for example:

print("Hello there!")

Some months ago I have discovered that the same could be done with

print'Hello there!'

and apparently 2 single quotes serves the same purpose as (“”)
I have also noticed that text inside it cannot be concatenated with the formal method “…”.
Possibly because single quotes serve the same purpose as (" ") The parenthesis close the string container.

Questions:
What are the advantages of ’ ’ over (“”) if there is any?
What is a good example with the usage of ’ '?
When exactly am I supposed to use it?

Thanks for any help!

4 Likes

No advantages or difference, just preference that’s it.

As for whether you should use parentheses or not, functions can take single string or table literals, if there’s a tuple then use parentheses.

print "hello" --> valid

local msg = "hello"
print msg --> invalid

print "hello", "world" -- invalid
print("hello, world") --> valid

A good usage of a string.
Whenever you’re using strings use "" , '' or [[]] for multiline strings - you don’t have other options to define strings simply.

2 Likes

Woah, didn’t know about all that!
This is definitively going to help me and hopefully others in the future :slight_smile:

1 Like

Electro mentioned multiline strings, which are basically strings that allow you to have the string expand more than a single line.

local multiStr = [[
hi
what's up?
]] --this is actually valid

--there is also a weird thing where you can add a = or more between the two [ and it's also valid

local multiStr = [=[
hi
what's up?
]=]
2 Likes

Fun fact you can have as many = as you want as long as the amount of = in between the opening brackets match the ones in the closing brackets.

I think the purpose of that was to allow the use of ]] because this is valid:

local d = [[ [[yes ]]

However this isn’t:

local d = [[ [[yes ]] ]]

(ignore the highlighter, it isn’t correct)

With the former the opening [[ are seen as contents of the string. However with the latter the ]] can only be seen as the end of the string.

To bypass this the = thing is done:

local d = [==[ [[yes ]] ]==]
5 Likes