How to break a big lua string into small ones

bratao picture bratao · Jul 22, 2011 · Viewed 22.4k times · Source

I have a big string (a base64 encoded image) and it is 1050 characters long. How can I append a big string formed of small ones, like this in C

 function GetIcon()
    return "Bigggg string 1"\
"continuation of string"\
"continuation of string"\
"End of string"

Answer

crashmstr picture crashmstr · Jul 22, 2011

According to Programming in Lua 2.4 Strings:

We can delimit literal strings also by matching double square brackets [[...]]. Literals in this bracketed form may run for several lines, may nest, and do not interpret escape sequences. Moreover, this form ignores the first character of the string when this character is a newline. This form is especially convenient for writing strings that contain program pieces; for instance,

page = [[
<HTML>
<HEAD>
<TITLE>An HTML Page</TITLE>
</HEAD>
<BODY>
 <A HREF="http://www.lua.org">Lua</A>
 [[a text between double brackets]]
</BODY>
</HTML>
]]

This is the closest thing to what you are asking for, but using the above method keeps the newlines embedded in the string, so this will not work directly.

You can also do this with string concatenation (using ..):

value = "long text that" ..
      " I want to carry over" ..
      "onto multiple lines"