Page 1 of 1

How to divide a multiline string into another strings? [Solved]

Posted: Sun May 12, 2019 8:50 pm
by Darlex
Something like:

Code: Select all

	input = [[
	Im a cool dude
	doesnt it?
	]]
	table = divide(input)
	printtable(table)
OUTPUT:
{"Im a cool dude","doesnt it?"}

Re: How to divide a multiline string into another strings?

Posted: Sun May 12, 2019 10:09 pm
by raidho36

Code: Select all

for line in string.gmatch ( str, "(.-)[\n\r]" ) do ...

Re: How to divide a multiline string into another strings?

Posted: Sun May 12, 2019 11:05 pm
by Darlex
raidho36 wrote: Sun May 12, 2019 10:09 pm

Code: Select all

for line in string.gmatch ( str, "(.-)[\n\r]" ) do ...
That's useful! THX

Re: How to divide a multiline string into another strings? [Solved]

Posted: Sun May 12, 2019 11:14 pm
by raidho36
You can use the following pattern if you want to ignore blank lines:

Code: Select all

"([^\r\n]+)"
Also I've noticed that the previous pattern requires a newline at the end of the string. Good thing it's a common practice to end all text files with a newline, most editors do this automatically.