views:

5400

answers:

9

What is the easiest way to capitalize the first letter in each word of a string?

+3  A: 

Take a look at the ucfirst function.

$line = join " ", map {ucfirst} split " ", $line;
zigdon
Note that split(" ", $line) splits on arbitrary whitespaces, so that won't preserve all whitespaces.
moritz
Agreed; the correct answer would be `$line = join " ", map {ucfirst} split / /, $line;` (note the " changed to /)
Ether
+10  A: 

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.

piCookie
Err, \U is internally implemented by calling ucfirst, so your statement about it contradicts your own advice. :-)
Aristotle Pagaltzis
The s///g switch iterates, so it does the whole string. When I first saw another answer with just "look at the ucfirst function" I felt at least more about split/join should be mentioned, and I see the person posting edited to include those already.
piCookie
You don't want to use the advice you gave, which is the code the FAQ uses to show the wrong way to do it. Read the text right after that bit where I explain why that answer is wrong. :)
brian d foy
I've now modified the FAQ to remove the wrong answer everyone is latching onto because they didn't read the second paragraph.
brian d foy
+1  A: 
$string =~ s/(\w+)/\u$1/g;

should work just fine

moritz
See <A href="http://faq.perl.org/perlfaq4.html#How_do_I_capitalize_">perlfaq4</a> to see why that's doesn't work just fine. :)
brian d foy
A: 

Note that the FAQ solution doesn't work if you have words that are in all-caps and you want them to be (only) capitalized instead. You can either make a more complicated regex, or just do a lc on the string before applying the FAQ solution.

The FAQ solution works just fine because there are actually several solution in the FAQ. The best solution is Damian's Text::Autoformat, which solves exactly your problem.
brian d foy
A: 

The ucfirst function in a map certainly does this, but only in a very rudimentary way. If you want something a bit more sophisticated, have a look at John Gruber's TitleCase script.

RET
Text::Autoformat does this already. It's in perlfaq4 :)
brian d foy
+10  A: 

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

Pat
A: 
$capitalized = join '', map { ucfirst lc } split /(\s+)/, $line;

By capturing the whitespace, it is inserted in the list and used to rebuild the original spacing. "ucfirst lc" capitalizes "teXT" to "Text".

kixx
A: 

You can use 'Title Case', its a very cool piece of code written in PERL.

vsync
A: 

see here related script of Capitalize Words

http://java.pakcarid.com/Cpp.aspx?sub=366&amp;ff=2954&amp;topid=34&amp;sls=25

lois