Click here to get back home

FAQ 4.30 How do I capitalize all the words on one line?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
FAQ 4.30 How do I capitalize all the words on one line? PerlFAQ Server 02-27-2008
Get Chitika Premium
Posted by PerlFAQ Server on February 27, 2008, 9:03 am
Please log in for more thread options
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.30: How do I capitalize all the words on one line?

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. Other times you might need a more
thorough solution (Suggested by brian d foy):

$string =~ s/ (
(^\w) #at the beginning of the line
| # or
(\s\w) #preceded by whitespace
)
/\U$1/xg;

$string =~ s/([\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
perllocale for endless details on locales.

This is sometimes referred to as putting something into "title case",
but that's not quite accurate. Consider the proper capitalization of the
movie *Dr. Strangelove or: How I Learned to Stop Worrying and Love the
Bomb*, for example.

Damian Conway's Text::Autoformat module provides some smart case
transformations:

use Text::Autoformat;
my $x = "Dr. Strangelove or: How I Learned to Stop ".
"Worrying and Love the Bomb";

print $x, "\n";
for my $style (qw( sentence title highlight )) {
print autoformat($x, { case => $style }), "\n";
}



--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Similar ThreadsPosted
FAQ 4.30: How do I capitalize all the words on one line? December 5, 2004, 12:03 am
FAQ 4.30: How do I capitalize all the words on one line? December 27, 2004, 12:03 pm
FAQ 4.30: How do I capitalize all the words on one line? January 5, 2005, 12:03 pm
FAQ 4.30 How do I capitalize all the words on one line? January 25, 2005, 12:03 pm
FAQ 4.30 How do I capitalize all the words on one line? May 21, 2005, 5:03 am
FAQ 4.30 How do I capitalize all the words on one line? August 5, 2005, 10:03 pm
FAQ 4.30 How do I capitalize all the words on one line? September 6, 2005, 10:03 am
FAQ 4.30 How do I capitalize all the words on one line? November 19, 2005, 5:03 am
FAQ 4.30 How do I capitalize all the words on one line? December 18, 2005, 11:03 pm
FAQ 4.30 How do I capitalize all the words on one line? April 16, 2006, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap