Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

What is the best way to capitalize a string?

by fundflow (Chaplain)
on Jul 14, 2000 at 23:53 UTC ( #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? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-05-17 01:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?