Fabrimat

TestWrap

Mar 6th, 2021 (edited)
679
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function wrap(str, limit)
  2.   local Lines, here, limit = {}, 1, limit or 72
  3.   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.
  4.  
  5.   str:gsub("(%s+)()(%S+)()",
  6.         function(sp, st, word, fi)  -- Function gets called once for every space found.
  7.           if fi-here > limit then
  8.                 here = st
  9.                 Lines[#Lines+1] = word                                             -- If at the end of a line, start a new table index...
  10.           else Lines[#Lines] = Lines[#Lines].." "..word end  -- ... otherwise add to the current table index.
  11.         end)
  12.  
  13.   return Lines
  14. end
  15.  
  16. local myText = "THIS IS A REALLY LONG TEXT THAT I WOULD LIKE TO WRAP AT A CERTAIN MARGIN."
  17.  
  18. local myTable = wrap(myText,20)
  19.  
  20. for k,v in pairs(myTable) do print(k.." = "..v) end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×