I am working on a library extension module, and I am looking for a way to swap casing of a given string with string.gsub
. If you use Python you'll know about str.swapcase()
. Basically I want to write that, but in Lua.
So if you pass a string "hELLO wORLD"
, string.swapcase("hELLO wORLD")
would yield "Hello World"
.
This is originally what I had:
-- this is a nested key-value pair, there is a main table, which this is in, but this is the only part u need tho string = setmetatable({ swapcase = function(s) return (s:gsub("%u" --[["%l"]])) -- i got stuck end }, { __index = string })
I was stuck here. Is there a way I can get the (upper/lower)case capture so I can change the casing to what it needs to be?
Thanks to @BashCaster, I found a solution. Everything is in this website: http://lua-users.org/wiki/StringRecipes