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( "tapered" )
local rmprefix = require( "rmprefix" )

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

t.done()

See also