Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I split multi-line strings?

Asked by 5 years ago

So I know you can make multi-line strings with "[[ ]]" or "\", but my question is how do we split those to make single line string and putting it in a table? This is what I want to do

local MultiLine = "Hey!\
This string\
is a multi-line one!"

local strings = {}
--Your fix

print(strings[1]) --Prints "Hey!"
print(strings[2]) --Prints "This string"
print(strings[3]) --Prints "is a multi-line one"

If you still don't understand, don't hesitate to ask :)

0
and yes, I have tried many methods, from string.sub to hexadecimals SirDerpyHerp 262 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Parsing

You can break a string into components by using patterns to find each desired chunk. String patterns follow a pretty simple format; it's just a matter of memorizing character classes, quantifiers, and some other specific mechanics. You can parse a string by using the gmatch function as an iterator to a generic for, using a specified pattern to find every next match.

local function parse(s)
    local t = {};
    for chunk in string.gmatch(s, "[^\n]+") do
        t[#t+1] = chunk;
    end;
    return t;
end;

The string pattern [^\n]+ will find all characters between every new line. You can read more about string patterns here.

0
Thanks dude! Your script works, and I will definitely look more into string patterns :D SirDerpyHerp 262 — 5y
0
Hey, I've seen the answer but I'm wondering how to detect when the string inbetween is "" (empty) as this script is not printing those out when I debug with it. GammaShock 32 — 5y
Ad

Answer this question