utils.split

Split a delimited string into a table

Prototype

t = utils.split (s, delim)

Description

The function utils.split 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: 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.

Lua functions

Topics