Split string in equal chunks in Lua
snippet in lua

Split string in equal chunks in Lua

user4922

local str = "0123456789" 
local sz = math.ceil(str:len() / 4)

Split string in equal chunks in Lua

user2379

local function splitByChunk(text, chunkSize)
    local s = {}
    for i=1, #text, chunkSize do
        s[#s+1] = text:sub(i,i+chunkSize - 1)
    end
    return s
end

-- usage example
local st = splitByChunk("0123456789",3)
for i,v in ipairs(st) do
   print(i, v)
end

-- outputs
-- 1    012
-- 2    345
-- 3    678
-- 4    9