Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

How do I capitalize all the words on one line?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:20 UTC ( #601=perlfaq nodetype: print w/ replies, xml ) Need Help??

Current Perl documentation can be found at

Here is our local, out-dated (pre-5.6) version:

To make the first letter of each word upper case:

        $line =~ s/\b(\w)/\U$1/g;

This has the strange effect of turning ``don't do it'' into ``Don'T Do It''. Sometimes you might want this, instead (Suggested by Brian Foy):

    $string =~ s/ (
                 (^\w)    #at the beginning of the line
                   |      # or
                 (\s\w)   #preceded by whitespace
    $string =~ /([\w']+)/\u\L$1/g;

To make the whole line upper case:

        $line = uc($line);

To force each word to be lower case, with the first letter upper case:

        $line =~ s/(\w+)/\u\L$1/g;

You can (and probably should) enable locale awareness of those characters by placing a use locale pragma in your program. See the perllocale manpage for endless details on locales.

Log In?

What's my password?
Create A New User
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2015-11-29 06:02 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (747 votes), past polls