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
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

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

Post 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?"}
Last edited by Darlex on Sun May 12, 2019 11:10 pm, edited 1 time in total.
Hi! I wish you have an amazing day!
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post by raidho36 »

Code: Select all

for line in string.gmatch ( str, "(.-)[\n\r]" ) do ...
Darlex
Party member
Posts: 128
Joined: Sun Sep 24, 2017 10:02 am
Location: Chile
Contact:

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

Post 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
Hi! I wish you have an amazing day!
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 81 guests