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

Spliting a string into table by new line?

Asked by
Filipalla 504 Moderation Voter
6 years ago
Edited 6 years ago

Soo what im trying to do is split a string with multiple lines into a table with each line. soo i searched on the wiki and it seems like i need to use the %s pattern the problem is that from my understanding that pattern is also for spaces is there a way for new lines only?

Im probably an idiot as i never use string patterns for anything

1 answer

Log in to vote
0
Answered by
luadotorg 194
6 years ago

For this you will have to do some patternizing, and it's a bit hard to do, but this will do.


function linesplit(inputstr) local t={} ; i=1 for str in string.gmatch(inputstr, "([^\n]+)") do t[i] = str i = i + 1 end return t end

There's unicode symbols that you can use when using patterns, such as "\n" which is new line. Notepad uses "\t", which is also another way of new line.

If you'd ever like to use a space, you have to use "%s".

You should go check some Unicode symbols and Patterns here: http://lua-users.org/wiki/SplitJoin

2
\t is a tab User#5423 17 — 6y
0
Thanks! I will check it out when i have time :) Filipalla 504 — 6y
Ad

Answer this question