30

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

brian d foy
  • 121,466
  • 31
  • 192
  • 551
Dan Littlejohn
  • 489
  • 2
  • 6
  • 7

10 Answers10

52

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

Eric Johnson
  • 16,234
  • 10
  • 49
  • 59
Pat
  • 34,832
  • 18
  • 68
  • 86
18

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.

smonff
  • 3,170
  • 2
  • 35
  • 44
piCookie
  • 8,912
  • 2
  • 17
  • 19
  • 2
    Err, \U is internally implemented by calling ucfirst, so your statement about it contradicts your own advice. :-) – Aristotle Pagaltzis Sep 16 '08 at 21:52
  • 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 Sep 16 '08 at 21:55
  • 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 Sep 18 '08 at 06:27
  • 5
    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 Sep 21 '08 at 22:42
13

Take a look at the ucfirst function.

$line = join " ", map {ucfirst} split " ", $line;
zigdon
  • 13,427
  • 6
  • 32
  • 54
  • 2
    Note that split(" ", $line) splits on arbitrary whitespaces, so that won't preserve all whitespaces. – moritz Oct 02 '08 at 20:00
  • 1
    Agreed; the correct answer would be `$line = join " ", map {ucfirst} split / /, $line;` (note the " changed to /) – Ether Oct 10 '09 at 03:21
  • @Ether: Closer, but it still strips _trailing_ spaces (and wouldn't recognize words separated by `\t` only). – mklement0 Oct 25 '15 at 03:55
  • A bigger problem is that `ucfirst` doesn't actually lowercase the string. Title case isn't `FOO` and this won't fix that. – user157251 Aug 09 '19 at 02:16
11
$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".

HoldOffHunger
  • 10,963
  • 6
  • 53
  • 100
kixx
  • 3,175
  • 21
  • 19
6
$string =~ s/(\w+)/\u$1/g;

should work just fine

moritz
  • 12,354
  • 1
  • 36
  • 62
2

This capitalizes only the first word of each line:

perl -ne "print (ucfirst($1)$2)  if s/^(\w)(.*)/\1\2/" file
Matt Fenwick
  • 44,546
  • 19
  • 115
  • 184
alemol
  • 21
  • 1
1

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

brian d foy
  • 121,466
  • 31
  • 192
  • 551
vsync
  • 87,559
  • 45
  • 247
  • 317
1

try this :

echo "what's the wrong answer?" |perl -pe 's/^/ /; s/\s(\w+)/ \u$1/g; s/^ //'

Output will be:

What's The Wrong Answer?
RavinderSingh13
  • 101,958
  • 9
  • 41
  • 77
bill god
  • 21
  • 1
1

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.

  • 1
    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 Sep 21 '08 at 22:24
-1

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.

brian d foy
  • 121,466
  • 31
  • 192
  • 551
RET
  • 8,960
  • 1
  • 25
  • 32