What’s the difference between creating a string with quotes and square brackets?

Asked

Viewed 280 times

4

I’ve always created the strings with the quotation marks, like this:

local minhaString = "Eu sou uma string"

I downloaded a source to give a study in the language and I came across the following way to create a string:

local query = [[
SELECT *
FROM `pointshop_data`
WHERE uniqueid = '%s'
]]

What is the advantage/difference in using the [[ / ]]? How I pass the parameter %s?

2 answers

4


The square bracketed shape is more crude, it essentially does not work with special characters and considers "everything" as text, including when skipping the line. It’s like using the @ of the C#.

To use arguments to be inserted in the text you must use the function string.format().

2

The main difference is that strings created with quotes accept escape sequences as \n and \t. Strings created with brackets do not interpret any escape sequence. See the manual.

The %s in your example indicates that probably the string will be used as format in string.format. Other than that, there is no special meaning.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.