Module:Sandbox/Ederporto/Test

Revision as of 17:17, 7 January 2019 by imported>Ederporto
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/Ederporto/Test/doc

-- Module test for splitting strings by some substring parsed

local p = {}

function p.split(frame)
	local text = frame.args.text or frame.args[1]
	local sep = frame.args.sep or frame.args[2]
	local nreturn = tonumber(frame.args.nreturn) or tonumber(frame.args[3])
	local out = {}
	local count = 1
	
	local from = 1
	local sep_from, sep_to = string.find(text, sep, from)
	while sep_from do
		if count == nreturn then
			return string.sub(text, from, sep_from-1)
		end
		from = sep_to+1
		sep_from, sep_to = string.find(text, sep, from)
		count = count + 1
	end
	if count == nreturn then
		return string.sub(text, from)
	end
end

return p