PDA

View Full Version : Substitution in words where first 'letter' becomes 'Letter'


bazz
07-13-2005, 05:23 PM
Hi,

$variable = "Full name";

I need to susbstitute the first letter in each word, to make sure that they are upper case.

I tried (seemingly feebly), $variable=~ s/[A-Z]/[a-z]/g; but only the first part works. It recognises the upper case letter but then just does a literal substitution for F with [a-z]. :(

please help.

Bazz

Kickin
07-13-2005, 10:48 PM
Have you tried

$variable = ucfirst($variable);

Or does the string have more than 1 word?

FishMonger
07-13-2005, 11:12 PM
$variable = "full name";

$variable =~ s/\b(\w)/\u$1/g;

bazz
07-14-2005, 05:58 PM
Excellent FishMonger,

That works a treat :) :thumbsup:

Bazz

cbowen2
07-14-2005, 07:16 PM
Here is a possible solution from the Perl Cookbook

$text = "thIS is a LoNG liNE";

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

The output will be: This Is A Long Line

Hope this helps.