2,875
edits
m (Ammend compareString function, converting inputs to strings) |
m (Add splitString param to trim whitespace) |
||
Line 155: | Line 155: | ||
end | end | ||
--Splits a string based on a sent in separating character | --- Splits a string based on a sent in separating character | ||
--For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"} | --- For example calling splitString ("Lith V1 Relic", " ") would return {"Lith", "V1", "Relic"} | ||
function p.splitString(inputstr, sep) | -- @param inputstr (string) The input to separate. | ||
-- @param sep (string/char) The separation character. | |||
-- @param trim (boolean) TRUE to trim the leading/trailing whitespaces | |||
function p.splitString(inputstr, sep, trim) | |||
if sep == nil then | if sep == nil then | ||
sep = "%s" | sep = "%s" | ||
Line 163: | Line 166: | ||
local t = {} | local t = {} | ||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do | for str in string.gmatch(inputstr, "([^"..sep.."]+)") do | ||
if trim == true then | |||
str = str:gsub("^%s*(.-)%s*$", "%1") | |||
end | |||
table.insert(t, str) | table.insert(t, str) | ||
end | end |
edits