Advertisement
PortalPlayer

lua equivalent of javascripts startsWith function

Jul 26th, 2020
2,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.68 KB | None | 0 0
  1. local startsWith = function(stringValue, searchValue, position)
  2.     if stringValue and searchValue then
  3.         if position then
  4.             return string.sub(string.sub(stringValue, position + 1), 1, #searchValue) == searchValue
  5.         else
  6.             return string.sub(stringValue, 1, #searchValue) == searchValue
  7.         end
  8.     else
  9.         return false
  10.     end
  11. end
  12.  
  13. -- example
  14. -- code | expected output
  15.  
  16. startsWith("testing oof", "testing") -- true
  17. startsWith("testing oof", "oof", 8)  -- true
  18.                                      --
  19. startsWith()                         -- false
  20. startsWith("testing oof")            -- false
  21. startsWith("testing oof", "oof")     -- false
  22. startsWith("testing oof", "oof", 10) -- false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement