Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I get the uppercase or lowercase capture in string.gsub?

Asked by 4 years ago
Edited 4 years ago

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?

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Thanks to @BashCaster, I found a solution. Everything is in this website: http://lua-users.org/wiki/StringRecipes

Ad

Answer this question