Module:StrManip

From Lo-BBA
Jump to navigation Jump to search

Documentation for this module may be created at Module:StrManip/doc

--This module contains string manipulation functions that lua should honestly just have by default.

------------------Split
function strSplit(str,sep)
    if sep == nil then
        sep = "%s"
    end
    local res = {}
    for i in string.gmatch(str,"([^"..sep.."]+)") do
        table.insert(res,i)
    end
    return res
end

function strSplitAtSeq(str,seq)
    local res = {}
    str = str.gsub(seq,"¨")
    for i in string.gmatch(str,"([^¨]+)") do
        table.insert(res,i)
    end
    return res
end

------------------Trim
function trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end


------------------In Table
function inTab(tab, val)
    for i, value in ipairs(tab) do
        if value == val then
            return true
        end
    end

    return false
end