First character uppercase Lua

2020-08-09 07:52发布

问题:

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.

回答1:

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


回答2:

This also works: s:sub(1,1):upper()..s:sub(2)



标签: string lua