43

I just have a general question.

Is there any difference using single and double quote marks within Lua?

Example:

require('test.lua')
require("test.lua")

When I programmed in PAWN, a language similar to C, single quote marks could be used for characters, but not strings of text, you had to use double quote marks for them.

And if there is no difference, which one is recommended to be used?

This question has most likely been answered already, however I failed to find a topic already answered.

Thank you.

Bicentric
  • 1,263
  • 3
  • 12
  • 12
  • No difference. Also if you're deciding to use `'` or `"` for array variables, you can just use dot notation. – Dave Chen Jun 19 '13 at 22:07
  • Ah okay thank you for your quick response, again, which one is more popularly used? – Bicentric Jun 19 '13 at 22:10
  • 1
    Just use single quotes when you have doubles quotes in the string, and single quotes when you have double quotes in the string. You can also escape in both of them using the backslash character, so it doesn't matter which one you pick. For popularity, it's just on preference because it truly doesn't matter. – Dave Chen Jun 20 '13 at 00:11

3 Answers3

57

Nope. No difference, except that you can enclose the other inside the ones that you are using.

-- No difference between these
myStr = "Hi!!"
myStr = 'Hi!!'
myStr = [[Hi!!]] -- The 'weird' way to make a string literal IMO...

-- Double quotes enclosed in single quotes    
myStr = 'My friend said: "Hi!!"'

-- Single quotes enclosed in double quotes
myStr = "My friend said: 'Hi!!'"

-- Single and double quotes enclosed in double brackets
myStr = [[My friend said: "What's up?"]]

See: Strings in Lua for more information.

AtinSkrita
  • 1,373
  • 12
  • 13
  • Ah okay, again thank you for your answer, it's greatly appreciated. However, I still would like to know which one is more common :) I am guessing the double quote mark is simply because strings in a wide variety of languages use double quote marks. – Bicentric Jun 19 '13 at 22:18
  • Yeah, in C, C++, C#, Java and many more languages double quotes are used for string literals, and single quotes are used for character literals. – AtinSkrita Jun 19 '13 at 22:25
  • 2
    @Bicentric you're thinking too much about it. Just use whichever one suits your fancy -- there is no preferred one over the other. I tend to use `'` because I don't have to hold an extra shift key when I type it. – greatwolf Jun 19 '13 at 23:16
  • 1
    I'd prefer not using `[[ ]]` because that will break comments over lines `--[[ line comment code ]]`. – Dave Chen Jun 20 '13 at 00:10
  • 2
    @DaveChen, not if you use the `--[==[` form. – lhf Jun 20 '13 at 01:39
  • Didn't know that! The only downside then, is if you're lazy to hit a key twice to enter string mode. – Dave Chen Jun 20 '13 at 01:50
  • You just saved my day. I am using a lua script embedded into a xml attribute and wanted to use single quotes inside a string. Typing 'hello 'world'' broke the script. Using [[ hello 'world' ]] worked fine for me. – NULL Sep 23 '14 at 17:21
4

There is no difference between the two in Lua, so you can use whichever you like. I tend to differentiate between them semantically:

  • I use "double quotes" for user-visible strings and text, basically whatever appears on the output or in files
  • I use 'single quotes' for option-like strings for passing to methods and generally in code, like io.stdout:setvbuf('no')
Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
2

You can find out about Lua strings easily enough by searching the internet for "lua strings".

Here is one result: http://www.lua.org/pil/2.4.html

You are free to choose your own quote style, as it makes no difference, but you should be consistent so that your code is nicer to read.

As a matter of preference, I would normally use double-quotes because Lua uses C-style escape-codes. Many script languages (Perl, for example) and command shells do make the distinction between single- and double-quoted strings, and it would be nice to be a little consistent with these quasi-standard practices.

I mean that when I see a single-quoted string I have to actually think about what language I'm reading, and whether or not escape-codes are substituted in that string. With double-quoted strings I don't have to think about it, because as a general rule any language that substitutes C-style escape-codes in a string will do that in a double-quoted string.

paddy
  • 60,864
  • 6
  • 61
  • 103
  • The PIL that is available online is now horribly outdated (bracket-delimiter `[[` nesting from 5.0 was replaced with `\[=*\]` syntax) it is usually better to refer to the manual: http://www.lua.org/manual/5.2/manual.html#3.1 – dualed Jun 21 '13 at 04:57