Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating variable into literal string

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.

like image 809
Urbley Avatar asked Feb 05 '14 15:02

Urbley


People also ask

Can you concatenate string literals?

A string literal is a sequence of characters, enclosed in double quotation marks (" ") , which is used to represent a null-terminated string in C/C++. If we try to concatenate two string literals using the + operator, it will fail. For instance, consider the following code, which results in a compilation error: C.

How do you concatenate a variable to a string in C++?

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.


1 Answers

The quotes inside the double bracket delimitors 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

like image 131
atlasologist Avatar answered Nov 06 '22 10:11

atlasologist