http://qs321.pair.com?node_id=22623

fundflow has asked for the wisdom of the Perl Monks concerning the following question: (strings)

Is there a better way to capitalize a string than
s/(\w+)/\u$1/g
?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: What is the best way to capitalize a string?
by plaid (Chaplain) on Jul 15, 2000 at 00:53 UTC
    A couple (better?) ways come to mind:
    $string = uc($string);
    $string =~ tr/a-z/A-Z/;
Re: What is the best way to capitalize a string?
by chromatic (Archbishop) on Jul 15, 2000 at 00:54 UTC
    Try the internal function uc. To capitalize just the first character, try ucfirst. This has the additional benefit of respecting any locale you have set.
Re: What is the best way to capitalize a string?
by galande (Initiate) on Nov 24, 2000 at 13:32 UTC
    You can use uc or lc built-in functions to change case. If you wanto to change case of first character only, then use ucfirst or lcfirst functions.

    $lower = "name"; $upper = "NAME"; print uc($lower),"\n"; ### This will print : NAME print lc($upper),"\n"; ### This will print : name print ucfirst($lower),"\n"; ## This will print : Name print lcfirst($upper),"\n"; ## This will print : nAME