Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break a big lua string into small ones

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" 
like image 831
bratao Avatar asked Jul 22 '11 19:07

bratao


People also ask

How do you break a string in Lua?

A very simple example of a split function in Lua is to make use of the gmatch() function and then pass a pattern which we want to separate the string upon.

What does string Sub Do in Lua?

Another important function of the Lua's string library is the string. sub() function. The string. sub() function is used to extract a piece of the string.

Can you index strings in Lua?

Note that there is no individual character indexing on strings, just like in Lua, so you can't use the indexing operator [] to read a single character. Instead, you need to get a substring to get a string of length 1 if you want individual characters (using sub in pico8, string.


1 Answers

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" 
like image 179
crashmstr Avatar answered Oct 04 '22 15:10

crashmstr