local text = "1-3-2"
-- gsub builds a new string, text stays as it was
local replaced = text:gsub("3", "x")
-- replaced = 1-x-2
-- text = 1-3-2
-- Swap two characters through an array of them
local chars = {}
for char in text:gmatch(".") do
chars[#chars + 1] = char
end
chars[3], chars[5] = chars[5], chars[3]
local swapped = table.concat(chars)
-- swapped = 1-2-3
print("replaced = " .. replaced)
print("text = " .. text)
print("swapped = " .. swapped)
-- Strings are immutable: no assignment by index
local ok, err = pcall(function()
text[3] = "2"
end)
-- ok = false
-- err = main.lua:23: attempt to index a string
-- value (upvalue 'text')
print("ok = " .. tostring(ok))
print("err = " .. err)