8

I'm trying to concatenate a variable into a literal string purely for readability purposes e.g.

myString = "test"
myString2 = [[
first part of the string
this is a " .. myString .. " string
last part of the string]]
print(myString2)

but this literally outputs

first part of the string
this is a " .. myString .. " string
last part of the string

I'm sure it's something simple but I've tried Googling to find out how to achieve this and came up blank.

1 Answer 1

12

The quotes inside the double bracket delimiters aren't doing anything. The only way to end the double bracket is with a double bracket:

myString2 = [[
first part of the string
this is a ]] .. myString .. [[ string
last part of the string]]

That will give you:

first part of the string
this is a test string
last part of the string

See: http://www.lua.org/pil/2.4.html

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly. Lua is a programming language based primarily on logic (as is the same with many other programming languages) and therefore all operators such as ( and [[ need to be matched with the opposite to that operator (in your case, ]]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.