Search code examples
stringlua

First character uppercase Lua


Does Lua provide a function to make the first character in a word uppercase (like ucfirst in php) and if so, how to use it?

I want keywords[1] to be first letter uppercase. I've read that string.upper does it but it made the whole word uppercase.


Solution

  • There are some useful string recipes here, including this one. To change the first character in a string to uppercase, you can use:

    function firstToUpper(str)
        return (str:gsub("^%l", string.upper))
    end