How make a function to change the first letter of one English sentence to upper

Posted by Lxhd on Tue 02 Feb 2016 03:05 AM — 3 posts, 14,314 views.

#0
I want to make a function by Lua to change the firt letter of one setence or word to upper case,
for examplle:
"i like you" to "I like you"
"woman" to "Woman"
please kindly give me your kind suggestion, thanks a lot.
Australia Forum Administrator #1
For the first letter of a sentence:


foo = string.gsub ("i like you", "^.", string.upper)

print (foo)  --> I like you


To capitalize every word you can use a (rather obscure) frontier pattern, like this:


foo = string.gsub ("i like you", "%f[%a].", string.upper)

print (foo)  --> I Like You


In both cases I am using a regular expression to find what we want to capitalize. Then we pass down the standard function string.upper to string.gsub, and that turns the matching text into upper case.
Amended on Tue 02 Feb 2016 04:52 AM by Nick Gammon
#2
Thanks a lot.