What is the easiest way to capitalize the first letter in each word of a string?
Perl | uc() Function uc() function in Perl returns the string passed to it after converting it into uppercase. It is similar to ucfirst() function which returns only first character in uppercase.
The upper() method converts all lowercase characters in a string into uppercase characters and returns it.
Convert the First Letter to uppercase You may use toUpperCase() method and convert the calling string to upper case.
As @brian is mentioning in the comments the currently accepted answer by @piCookie is wrong!
$_="what's the wrong answer?"; s/\b(\w)/\U$1/g print;
This will print "What'S The Wrong Answer?" notice the wrongly capitalized S
As the FAQ says you are probably better off using
s/([\w']+)/\u\L$1/g
or Text::Autoformat
See the faq.
I don't believe ucfirst()
satisfies the OP's question to capitalize the first letter of each word in a string without splitting the string and joining it later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With