Split string in Lua?

321,616

Solution 1

If you are splitting a string in Lua, you should try the string.gmatch() or string.sub() methods. Use the string.sub() method if you know the index you wish to split the string at, or use the string.gmatch() if you will parse the string to find the location to split the string at.

Example using string.gmatch() from Lua 5.1 Reference Manual:

 t = {}
 s = "from=world, to=Lua"
 for k, v in string.gmatch(s, "(%w+)=(%w+)") do
   t[k] = v
 end

Solution 2

If you just want to iterate over the tokens, this is pretty neat:

line = "one, two and 3!"
for token in string.gmatch(line, "[^%s]+") do
   print(token)
end

Output:

one,

two

and

3!

Short explanation: the "[^%s]+" pattern matches to every non-empty string in between space characters.

Solution 3

If you program in Lua, you are out of luck here. Lua is THE one programming language that just happens to be notoriously infamous because its authors never implemented "the" split function in the standard library, and instead wrote 16 screenfulls of explanations and lame excuses as to why they didn't and wouldn't, interspersed with numerous half-working examples that are virtually guaranteed to work for almost everyone but break in your corner case. This is just Lua state of the art, and everyone who programs in Lua simply ends up clenching their teeth and iterating over characters. There are lots of solutions in existence that are sometimes better, but exactly zero solutions that are reliably better.

Solution 4

Just as string.gmatch will find patterns in a string, this function will find the things between patterns:

function string:split(pat)
  pat = pat or '%s+'
  local st, g = 1, self:gmatch("()("..pat..")")
  local function getter(segs, seps, sep, cap1, ...)
    st = sep and seps + #sep
    return self:sub(segs, (seps or 0) - 1), cap1 or sep, ...
  end
  return function() if st then return getter(st, g()) end end
end

By default it returns whatever is separated by whitespace.

Solution 5

Here is the function:

function split(pString, pPattern)
   local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
     table.insert(Table,cap)
      end
      last_end = e+1
      s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
      cap = pString:sub(last_end)
      table.insert(Table, cap)
   end
   return Table
end

Call it like:

list=split(string_to_split,pattern_to_match)

e.g.:

list=split("1:2:3:4","\:")


For more go here:
http://lua-users.org/wiki/SplitJoin

Share:
321,616

Related videos on Youtube

RCIX
Author by

RCIX

Lua is underrated!

Updated on July 13, 2020

Comments

  • RCIX
    RCIX over 2 years

    I need to do a simple split of a string, but there doesn't seem to be a function for this, and the manual way I tested didn't seem to work. How would I do it?

  • Darius Bacon
    Darius Bacon over 12 years
    +1. Note to any other Lua beginners: this returns an iterator, and 'between patterns' includes the beginning and end of the string. (As a newbie I had to try it to figure these things out.)
  • hexagonest about 9 years
    This is my favorite, since it's so short and simple. I don't quite understand what happens, could someone explain to me?
  • Lars Gyrup Brink Nielsen
    Lars Gyrup Brink Nielsen almost 9 years
    The pattern %S is equal to the one you mentioned, as %S is the negation of %s, like %D is the negation of %d. Additionally, %w is equal to [A-Za-z0-9_] (other characters might be supported depending on your locale).
  • TurboHz
    TurboHz over 8 years
    This fails when using dot as delimiter (or potentially any other pattern magic character)
  • Szczepan Hołyszewski
    Szczepan Hołyszewski 10 months
    @Mud You miss the point. Every algorithm can be implemented in a Turing complete language, so ANYONE CAN just port JS's split() or PHP's explode() or whatever. But with Lua authors refusing to standardize ONE solution for Lua, EVERYONE HAS TO. And that's thousands upon thousands of programmer-hours accumulating over decades. And ONLY LUA AUTHORS can standardize a solution!
  • Gras Double
    Gras Double 9 months
    I thought this answer was a bit harsh and trolling, but it's true actually. For readers interested by the "16 screenfulls of explanations (…)": Split Join.

Related