There might be an easier way but I would use split and sub, not gsub:
local function toProper(string)
local split = string:split(' ')
for i,v in ipairs(split) do
split[i] = v:sub(1, 1):upper()..v:sub(2):lower()
end
return table.concat(split, ' ')
end
local function convertToTitle(s)
s = s:gsub("^(.)", function(c) return c:upper() end)
s = s:gsub("(%s.)", function(c) return c:upper() end)
return s
end
print(convertToTitle("random string here")) --Random String Here
Unfortunately Lua string patterns don’t allow for logic operators in the same way Regex does so I wasn’t able to write a single-liner.