Add string split function to gears.string

This commit is contained in:
romildo 2017-03-17 21:13:11 -03:00 committed by Emmanuel Lepage Vallee
parent 088584c812
commit 894c254c42
1 changed files with 22 additions and 0 deletions

View File

@ -84,4 +84,26 @@ function gstring.query_to_pattern(q)
end)
return s
end
--- Split separates a string containing a delimiter into the list of
-- substrings between that delimiter.
-- @class function
-- @name split
-- @tparam string str String to be splitted
-- @tparam string delimiter Character where the string will be splitted
-- @treturn table list of the substrings
function gstring.split(str, delimiter)
local pattern = "(.-)" .. delimiter .. "()"
local result = {}
local n = 0
local lastPos = 0
for part, pos in string.gmatch(str, pattern) do
n = n + 1
result[n] = part
lastPos = pos
end
result[n + 1] = string.sub(str, lastPos)
return result
end
return gstring