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


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?


How do I capitalize all the words in a string?


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
 $sentence =~ /([\w']+)/\u\L$1/g;

Generated 16:01:28 on Feb 21, 2020