startswithany

function startswithany( str, tab ) --> res

Description

Checks if str starts with one of the values in tab.

Parameters

str

The string that should be checked.

tab

An array table with strings.

Return Values

res

Is true if the string starts with one of the values in tab, otherwise false.

Code

--ZFUNC-startswithany-v1
local function startswithany( str, tab ) --> res
   for _, v in ipairs( tab ) do
      local sub = string.sub( str, 1, string.len( v ) )
      if sub == v then
         return true
      end
   end

   return false
end

return startswithany

Examples

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

str = "abcdef"
t( startswithany( str, { "def", "abc" } ), true )
t( startswithany( str, { "cba", "fed" } ), false )

t()