Eliot’s Tips and Tricks

Splitting strings

Note: In Iguana version 5.6.16 we added the string.split() function to Iguana’s standard string library to split strings on a specified delimiter, this removes the stringutil module dependency.

If you are using a version before 5.6.16 you can use string.split() function in the stringutil module.

Lua does not have a built in ‘split’ function like many other languages do like Python and Javascript. Not to worry. It’s extremely easy to add one using:

function string.split(s,d)
   local t = {}
   local i = 0
   local f
   local match = '(.-)' .. d .. '()'
   if string.find(s, d) == nil then
      return {s}
   end
   for sub, j in string.gfind(s, match) do
         i = i + 1
         t[i] = sub
         f = j
   end
   if i~= 0 then
      t[i+1]=string.sub(s,f)
   end
   return t
end

Where:

s – stands for a string

d – stands for delimiter

Tip: The string.split() function is now included in the stringutil module which can be downloaded from our code server.

Tagged:

One Comment

  1. Pingback: Will You Share Your Code? - iNTERFACEWARE Inc.

Leave A Comment?