rmprefix

function rmprefix( str, prefix ) --> nstr

Description

If str starts with prefix returns the function a new string without the prefix, otherwise str itself.

Parameters

str

The string that should lose the prefix.

prefix

The prefix that should be removed.

Return Values

nstr

A new string without the prefix.

Code

--ZFUNC-rmprefix-v1
local function rmprefix( str, prefix ) --> nstr
   local prefixlen = string.len( prefix )
   local startsub = string.sub( str, 1, prefixlen )
   if startsub == prefix then
      return string.sub( str, prefixlen + 1 )
   else
      return str
   end
end

return rmprefix

Examples

local t = require( "taptest" )
local rmprefix = require( "rmprefix" )

t( rmprefix( "abc.def", "abc." ), "def" )
t( rmprefix( "abc.def", "def." ), "abc.def" )
t( rmprefix( "abc.abc.def", "abc." ), "abc.def" )

t()

See also