Dave's Brain

Browse - Programming Tips - How do I convert a string to title case with Perl?

Date: 2008jan14
Language: perl

Q.  How do I convert a string to title case with Perl?

A.  Title Case Is Where Each Word Begins With An Uppercase Letter.
    Use this replacement:

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

For each word (\w+) use the \u\L command.
This is exactly the kind of thing that Perl was made for!

Some related functions:

$title = uc($title);	# To UPPERCASE
$title = lc($title);	# To lowercase
$title = ucfirst($title) # First letter to Uppercase
$title = lcfirst($title) # First letter to lowercase

Add a comment

Sign in to add a comment
Copyright © 2008-2016, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.