1-- 2-- string.lua 3-- Additions to Lua's built-in string functions. 4-- Copyright (c) 2002-2008 Jason Perkins and the Premake project 5-- 6 7 8-- 9-- Returns an array of strings, each of which is a substring of s 10-- formed by splitting on boundaries formed by `pattern`. 11-- 12 13 function string.explode(s, pattern, plain) 14 if (pattern == '') then return false end 15 local pos = 0 16 local arr = { } 17 for st,sp in function() return s:find(pattern, pos, plain) end do 18 table.insert(arr, s:sub(pos, st-1)) 19 pos = sp + 1 20 end 21 table.insert(arr, s:sub(pos)) 22 return arr 23 end 24 25 26 27-- 28-- Find the last instance of a pattern in a string. 29-- 30 31 function string.findlast(s, pattern, plain) 32 local curr = 0 33 repeat 34 local next = s:find(pattern, curr + 1, plain) 35 if (next) then curr = next end 36 until (not next) 37 if (curr > 0) then 38 return curr 39 end 40 end 41 42 43 44-- 45-- Returns true if `haystack` starts with the sequence `needle`. 46-- 47 48 function string.startswith(haystack, needle) 49 return (haystack:find(needle, 1, true) == 1) 50 end 51