2014-02-05 78 views
7

我試圖將變量連接成文字字符串,純粹出於可讀性目的將變量連接成文字字符串

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

但這字面上輸出

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

我敢肯定這件事情簡單,但我已經嘗試使用Google找到了如何實現這一點,想出了空白。

回答

11

雙括號分隔符內的引號沒有做任何事情。結束雙支架的唯一方法是用雙支架:

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

這會給你:

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

參見:http://www.lua.org/pil/2.4.html

+0

沒錯。 Lua是一種主要基於邏輯的編程語言(與許多其他編程語言一樣),因此所有運算符(例如'('和'[[''需要與該運算符相反)(在您的情況下,'' ]]')。 – cybermonkey