Search code examples
luapattern-matchinguppercaselowercaselua-patterns

Lua pattern replace uppercase letters


I need a special Lua pattern that takes all the uppercase letters in a string, and replaces them with a space and the respective lowercase letter;

TestStringOne => test string one
this isA TestString => this is a test string

Can it be done?


Solution

  • Assuming only ASCII is used, this works:

    function lowercase(str)
      return (str:gsub("%u", function(c) return ' ' .. c:lower() end))
    end
    
    print(lowercase("TestStringOne"))
    print(lowercase("this isA TestString"))