Splitting strings at a delimiter

Posted by Nick Gammon on Mon 14 Nov 2005 02:57 AM — 9 posts, 48,807 views.

Australia Forum Administrator #0
Version 3.69 of MUSHclient adds a new Lua script function: utils.split. This was suggested by Ked.

This is intended to do the reverse of table.concat. That is, it takes a string and generates a table of entries, delimited by single-character delimiters (such as comma or newline).

Example:


test = "the,quick,brown,dog,jumped"  

t = utils.split (test, ",")

tprint (t)

print (table.concat (t, ","))

Output:

1="the"
2="quick"
3="brown"
4="dog"
5="jumped"

the,quick,brown,dog,jumped


You pass utils.split 2 or 3 arguments:

  1. The string to be split
  2. The single-character delimiter
  3. (optional) the maximum number of splits to do



If the 3rd argument is not supplied, or is zero, then the entire string is split. Otherwise, it will be split the number of times you specify. eg.


t = utils.split (test, ",", 2)

tprint (t)

Output:

1="the"
2="quick"
3="brown,dog,jumped"


In this case the remaining text is placed in the 3rd table item.
Amended on Mon 14 Nov 2005 07:33 PM by Nick Gammon
Australia Forum Administrator #1
The code for the split routine is:


static int l_split (lua_State *L) {
  const char *s = luaL_checkstring(L, 1);
  const char *sep = luaL_checkstring(L, 2);
  const int count = (int) luaL_optnumber (L, 3, 0);
  char *e;
  int i = 1;

  if (strlen (sep) != 1)
    luaL_error (L, "Separator must be a single character");

  if (count < 0)
    luaL_error (L, "Count must be positive or zero");

  lua_newtable(L);  /* result */

  /* repeat for each separator */
  while ((e = strchr(s, *sep)) != NULL && 
         (count == 0 || i <= count)) 
    {
    lua_pushlstring(L, s, e-s);  /* push substring */
    lua_rawseti(L, -2, i++);
    s = e + 1;  /* skip separator */
    }

  /* push last substring */
  lua_pushstring(L, s);
  lua_rawseti(L, -2, i);

  return 1;  /* return the table */
}
Amended on Mon 14 Nov 2005 07:34 PM by Nick Gammon
Australia Forum Administrator #2
The equivalent code in Lua is:


function split (s, delim)

  assert (type (delim) == "string" and string.len (delim) > 0,
          "bad delimiter")

  local start = 1
  local t = {}  -- results table

  -- find each instance of a string followed by the delimiter

  while true do
    local pos = string.find (s, delim, start, true) -- plain find

    if not pos then
      break
    end

    table.insert (t, string.sub (s, start, pos - 1))
    start = pos + string.len (delim)
  end -- while

  -- insert final one (after last delimiter)

  table.insert (t, string.sub (s, start))

  return t
 
end -- function split



This version does not incorporate the "count" argument, although you could add it easily enough. However unlike the version in the utils library, it will handle a multi-character delimiter.
Amended on Fri 28 Apr 2006 02:03 AM by Nick Gammon
#3
hola mushClient people,

can i split strings without a seperator?

like
var theString = "abcde"
var myArray

to

myArray = split.theString

myArray[0] = "a"
myArray[1] = "b"
myArray[2] = "c"
myArray[3] = "d"
myArray[4] = "e"

???

is there a easy way? or do i have to write a new function which does that?

thanks

USA #4
That doesn't look like Lua, Hasz. Are you using JScript? If so, you can just use var t = "foo".split('').

In the case of Lua, just do this:
local s = "foo"

local t = {}
for i=1,#s do
  t[i] = s:sub(i,i)
end
Amended on Sat 17 Mar 2012 04:24 PM by Twisol
#5
ok nice.
iam useing lua right now but have a little bit more experience in javascript.

thx jonathan
#6
Nick Gammon said:

The equivalent code in Lua is:

-----
function split (s, delim)

  assert (type (delim) == "string" and string.len (delim) > 0,
          "bad delimiter")

  local start = 1
  local t = {}  -- results table

  -- find each instance of a string followed by the delimiter

  while true do
    local pos = string.find (s, delim, start, true) -- plain find

    if not pos then
      break
    end

    table.insert (t, string.sub (s, start, pos - 1))
    start = pos + string.len (delim)
  end -- while

  -- insert final one (after last delimiter)

  table.insert (t, string.sub (s, start))

  return t
 
end -- function split
-----

This version does not incorporate the "count" argument, although you could add it easily enough. However unlike the version in the utils library, it will handle a multi-character delimiter.



I could probably use this to create tables for my lines that are delimited by "|" characters, I'm going to give it a try.
USA Global Moderator #7
I think a neater string:split function in Lua is (leaving in the ability to use actual patterns instead of just plain strings):


function string:split(pat)
   local fields = {}
   local start = 1
   self:gsub("()("..pat..")", 
      function(c,d)
         table.insert(fields,self:sub(start,c-1))
         start = c + #d
      end
   )
   table.insert(fields, self:sub(start))
   return fields
end

Amended on Sun 28 Jul 2013 04:14 AM by Fiendish
Australia Forum Administrator #8
Yes, very neat. :)