lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


LuaTeX adds this function to the string library:
--[[
string.explode(s[,m]). This function returns an array containing the 
string argument s split into sub-strings based on the value of the 
string argument m. The second argument is a string that is either 
empty (this splits the string into characters), a single character
(this splits on each occurrence of that character, possibly introducing 
empty strings), or a single character followed by the plus sign + 
(this special version does not create empty sub-strings). The default 
value for m is ' +' (multiple spaces).
--]]

This function as it stands should not be added to the standard library.
However, an iterator function string.split(s,[,m]) such that 
string.explode is equivalent to

function(str,delim)
    local t={}
    for k in str:split(delim) do t[#t+1]=k end
    return t
    end

maybe with delim not as restricted as in LuaTeX (i.e. normal string
patterns allowed) would be handy in many other applications too.
You can sort of mock it up using existing string functions but it
is surprisingly tricky to get it right.

Dirk