Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function wrap(str, limit)
- local Lines, here, limit = {}, 1, limit or 72
- Lines[1] = string.sub(str,1,str:find("(%s+)()(%S+)()")-1) -- Put the first word of the string in the first index of the table.
- str:gsub("(%s+)()(%S+)()",
- function(sp, st, word, fi) -- Function gets called once for every space found.
- if fi-here > limit then
- here = st
- Lines[#Lines+1] = word -- If at the end of a line, start a new table index...
- else Lines[#Lines] = Lines[#Lines].." "..word end -- ... otherwise add to the current table index.
- end)
- return Lines
- end
- local myText = "THIS IS A REALLY LONG TEXT THAT I WOULD LIKE TO WRAP AT A CERTAIN MARGIN."
- local myTable = wrap(myText,20)
- for k,v in pairs(myTable) do print(k.." = "..v) end
Advertisement
Add Comment
Please, Sign In to add comment