Module:Sandbox/Element10101/Split
Jump to navigation
Jump to search
Usage
[edit source]{{#invoke:Split|split|string|separator}} splits a string between strings separated by the separator, which defaults to a space.
local p = {}
p.split = function (frame)
inputstr = frame.args[1]
sep = frame.args[2]
-- if sep is null, set it as space
if sep == nil then
sep = '%s'
end
-- define an array
local t = {}
-- split string based on sep
for str in string.gmatch(inputstr, '([^'..sep..']+)') do
-- insert the substring in table
table.insert(t, str)
end
-- return the array
return t
end
return p