lua-users home
lua-l archive

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


> What I need the most is tokenizing functions, for
> trimming leading/trailing spaces off a string, and removing words one at a
> time.

How about the following:

-- strip leading and trailing white space from a line
function strip(l)
    return gsub(l, "%s*(.*)%s*", "%1", 1)
end

-- remove a prefix p from s
-- return the beheaded s and the removed prefix
function behead(s, p)
    local s2, z

    z, z, p, s2 = strfind(s, p .. ")(.*)")
    return s2 or s, p
end

You can use

s = strip(s)
s, token = behead(s, "%w")

to remove words from a string.

-- 
http://sc3d.org/rrt/ | perfect, a.  unsatirizable