DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Capitalize all the words in a string?

Question:

How do I capitalize all the words in a string?

Answer:

You could write a few lines of code or simply use a regular expression. The second solution might be more what you're looking for.

# Capitalizes all words - this will turn 
 #      don't feed ducks
 # into 
 #      Don'T Feed Ducks
 #          ^--!
 
 $sentence =~ s/\b(\w)/\U$1/g;
 
 
 # a better solution, taking care of such situations:
 $sentence =~ s/(
                  (^\w)    #at the beginning of the line
                    |      # or
                  (\s\w)   #preceded by whitespace
                )
               /\U$1/xg;
 $sentence =~ /([\w']+)/\u\L$1/g;
 

Generated 0:01:49 on Dec 15, 2017