Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

What is the best way to capitalize a string?

by fundflow (Chaplain)
on Jul 14, 2000 at 23:53 UTC ( [id://22623]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://22623]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-04-19 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found