CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Frequently Asked Questions:
How do I capitalize all the words on one line?

 



Jasmine
Administrator

Jan 19, 2001, 2:53 PM

Post #1 of 1 (1590 views)
How do I capitalize all the words on one line? Can't Post

(From the Perl FAQ)

How do I capitalize all the words on one line?

To make the first letter of each word upper case:

Code
        $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):

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

To make the whole line upper case:

Code
        $line = uc($line);

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

Code
        $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.



 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives