Modul:String

Aus FürthWiki

Unterseiten

Siehe auch


local p = {}

function p.replace(s, old, new, count)
    local result
    -- Wenn count angegeben ist, wird string.gsub mit dem limitierten Ersetzungszähler verwendet
    if count then
    	result = string.gsub(s, old, new, count)
    else
        -- Wenn kein count angegeben ist, wird string.gsub alle Vorkommen ersetzen
        result = string.gsub(s, old, new)
    end
    return result
end

function p.split(s, delimiter)
	delimiter = delimiter or " "
    local result = {}
    local start = 1
    local delim_start, delim_end = string.find(s, delimiter, start, true)  -- true = plain find (keine Patterns)

    while delim_start do
        table.insert(result, string.sub(s, start, delim_start - 1))
        start = delim_end + 1
        delim_start, delim_end = string.find(s, delimiter, start, true)
    end
    table.insert(result, string.sub(s, start))
	
	--[[Debugging only 
	for _, value in ipairs(result) do
    	mw.log(value)  -- oder print(value), wenn du es direkt in der Konsole sehen möchtest
	end]]
    return result
end

function p.strip(s)
	-- Removes all spaces at the start and at the end of a string
    return s:match("^%s*(.-)%s*$")
end

function p.lstrip(s)
	-- Removes all spaces at the start of a string
	return s:match("^%s*(.-)$")
end

function p.rstrip(s)
	-- Removes all spaces at the end of a string
	return s:gsub("%s+$", "")
end

return p