lua split text code example

Example 1: lua string.split

function Split(s, delimiter)
    result = {};
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match);
    end
    return result;
end

split_string = Split("Hello World!", " ")
-- split_string[1] = "Hello"
-- split_string[2] = "World!"

Example 2: lua string split

function stringsplit(inputstr, sep)
	if sep == nil then
		sep = "%s"
	end
	local t={}
	i=1
	for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
		t[i] = str i = i + 1
	end
	return t
end

Tags:

Lua Example