Skip to content

Instantly share code, notes, and snippets.

@scawp
Created November 22, 2021 13:37
Show Gist options
  • Save scawp/1f08005ea3486b74b85f401906123226 to your computer and use it in GitHub Desktop.
Save scawp/1f08005ea3486b74b85f401906123226 to your computer and use it in GitHub Desktop.
--Ended up using string.gmatch in my project instead, adding here for reference
--License MIT or original?
function string_explode(str, div, limit) --extention of https://love2d.org/wiki/String_exploding
assert(type(str) == "string" and type(div) == "string", "invalid arguments")
limit = limit or 0 -- 0/1/nil = no limit, n = n or all
local o = {}
while true do
local pos1,pos2 = str:find(div)
if not pos1 then
o[#o+1] = str
break
end
o[#o+1],str = str:sub(1,pos1-1),str:sub(pos2+1)
if #o == (limit-1) then
o[#o+1] = str
break
end
end
return o
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment