2014-02-05 3 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 

출력합니다.

답변

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

. 루아는 주로 로직을 기반으로하는 프로그래밍 언어이므로 (다른 많은 프로그래밍 언어와 동일하므로)'('와'[[''연산자는 그 연산자와 반대가 될 필요가 있습니다 (' ]]). – cybermonkey

관련 문제