Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I split multi-line strings?

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 :)

1 comment

0

and yes, I have tried many methods, from string.sub to hexadecimals

1 answer

✓ Accepted answer

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.

2 comments

0

Thanks dude! Your script works, and I will definitely look more into string patterns :D

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.