Created
November 22, 2021 13:37
-
-
Save scawp/1f08005ea3486b74b85f401906123226 to your computer and use it in GitHub Desktop.
Added Limit to https://love2d.org/wiki/String_exploding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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