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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Darlex
Party member
Posts: 118
Joined: Sun Sep 24, 2017 10:02 am
Location: absolutely not Chile
Contact:

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

Post by Darlex » Sun May 12, 2019 8:50 pm

Something like:

Code: Select all

	input = [[
	Im a cool dude
	doesnt it?
	]]
	table = divide(input)
	printtable(table)
OUTPUT:
{"Im a cool dude","doesnt it?"}
Last edited by Darlex on Sun May 12, 2019 11:10 pm, edited 1 time in total.
culdud uwu

User avatar
raidho36
Party member
Posts: 1951
Joined: Mon Jun 17, 2013 12:00 pm

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

Post by raidho36 » Sun May 12, 2019 10:09 pm

Code: Select all

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

User avatar
Darlex
Party member
Posts: 118
Joined: Sun Sep 24, 2017 10:02 am
Location: absolutely not Chile
Contact:

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

Post by Darlex » Sun May 12, 2019 11:05 pm

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
culdud uwu

User avatar
raidho36
Party member
Posts: 1951
Joined: Mon Jun 17, 2013 12:00 pm

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

Post by raidho36 » Sun May 12, 2019 11:14 pm

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.

Post Reply

Who is online

Users browsing this forum: Google [Bot], ivan and 13 guests