Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a splitString funtion to textutils #1768

Closed
wants to merge 10 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,39 @@ local function ensure_width(line, width)
return line
end

--- Splits a string in a table.
--
-- @tparam string inputstr The string to split.
-- @tparam string sep The separator to split the string at
-- @tparam number split_count How many times the string should be splitted.
-- @treturn table A table containing the splitted strings.
-- @usage args = textutils.splitString("arg1 arg2", " ")
-- @since edit this if it goes live (I hope it does)
local function split(inputstr, sep, split_count)
expect(1, inputstr, "string")
expect(2, sep, "string", "nil")
expect(3, split_count, "number", "nil")
if sep == nil then
sep = "%s"
end
if split_count == nil then
split_count = -1
end
local splitted_table = {}
local splitted_amount = 0
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
if splitted_amount == split_count then
break
else
table.insert(splitted_table, str)
splitted_amount = splitted_amount + 1
end
end
return splitted_table
end

return {
wrap = wrap,
ensure_width = ensure_width,
split = split,
}
Loading