string_split.lua

Send to Kindle
home » snippets » lua » string_split.lua


-- TODO(chirayu): What's a nice way to parameterize this so the separator/comma is also a parameter?
local function iterate_comma_values(csv, idx_previous_comma)
  idx_comma = string.find(csv, ",", idx_previous_comma + 1, true)
  if idx_comma == nil then
    return nil
  end
  return idx_comma, string.sub(csv, idx_previous_comma + 1, idx_comma - 1)
end


io.write("Iterator in for loop: Split by commas\n")
for start_index, value in iterate_comma_values, ",abc,def,ghi,", 1 do
  io.write(string.format("index: %d, value: %s\n", start_index, value))
end
io.write("\n")
--[[
Displays
  5       abc
  9       def
  13      ghi
]]--


local function iterate_comma_values_of_key_value_pairs(csv, idx_previous_comma)
  idx_comma = string.find(csv, ",", idx_previous_comma + 1, true)
  if idx_comma == nil then
    return nil
  end
  piece = string.sub(csv, idx_previous_comma + 1, idx_comma - 1)
  return idx_comma, "constant_A", piece, "constant_B"
end


io.write("Iterator in for loop: Split by commas and returning more than 1 value\n")
for start_index, v1, v2, v3 in iterate_comma_values_of_key_value_pairs, ",abc,def,ghi,", 1 do
  io.write(string.format("index: %d, value: %s, %s, %s\n", start_index, v1, v2, v3))
end
io.write("\n")
--[[
Displays
  5       abc
  9       def
  13      ghi
]]--





-- Splitting at underscores (older code)

function split_string_at_underscores(str)
  result = { }
  if not str then
    return result
  end
  -- Array/Table indices start at 1.
  local idx_start = 1
  while true do
    -- Pass true for 4th param to indicate that pattern is not a regex.
    idx_sep = string.find(str, '_', idx_start, true)
    if not idx_sep then
      part = string.sub(str, idx_start)
      if #part > 0 then result[#result + 1] = part end
      return result
    end
    --  substring function string.sub takes both inclusive indices.
    part = string.sub(str, idx_start, idx_sep - 1)
    if #part > 0 then io.write(string.format("Appending: [%d, %d] = %s\n", idx_start, idx_sep, string.sub(str, idx_start, idx_sep - 1))) end
    if #part > 0 then result[#result + 1] = string.sub(str, idx_start, idx_sep - 1) end
    idx_start = idx_sep + 1
  end
end

results = split_string_at_underscores("__ab__c_d_e__")

io.write("\n")
for i = 1, #results do
  io.write(string.format("[%d] = %s\n", i, results[i]))
end