Module:StringHelper

Revision as of 22:46, 5 August 2013 by module>ATDT (add line iterator)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

-- copy mw.ustring methods
for k, v in pairs( mw.ustring ) do p[k] = v end

-- true if string 's' starts with string 'prefix'
function p.startswith( s, prefix )
   return p.sub( s, 1, p.len( prefix ) ) == prefix
end

-- true if string 's' ends with string 'suffix'
function p.endswith( s, suffix )
   return suffix == '' or p.sub( s, 0 - p.len( suffix ) ) == suffix
end

-- strip characters 'chars' from beginning and end of string 's'
function p.strip( s, chars )
    s = p.gsub( s, '^[' .. chars .. ']*', '' )
    s = p.gsub( s, '[' .. chars .. ']*$', '' )
    return s
end

-- line iterator
function p.lines( s )
    return p.gmatch( s, "[^\r\n]+" )
end

return p