
February 10th, 2003, 04:14 PM
|
 |
Contributing User
|
|
Join Date: Jun 2000
Location: Southern California
Posts: 73
Time spent in forums: 2 m 24 sec
Reputation Power: 10
|
|
I'm not sure exactly what you are trying to achieve, so I'll give a solution for several scenarios:
1). You have a lowercase string, and you want the first character uppercase:
Code:
$str = ucfirst($str);
2). You have a lowercase string, and you want to change all "words" to have their first character uppercase:
(Note that you need the '(?<!\')' to keep "isn't" from becoming "Isn'T")...
Code:
my $s = q{this is a sentence};
my $s2 = q{this isn't a sentence};
$s =~ s/(?<!\')\b(\w+)/\u$1\E/g;
$s2 =~ s/(?<!\')\b(\w+)/\u$1\E/g;
print $s, "\n"; #==> This Is A Sentence
print $s2, "\n"; #==> This Isn't A Sentence
3). You have a string in which you want to capitalize the first character of a certain word:
Code:
$s = 'going to capitalize foo now';
$s =~ s/\b(foo)\b/\u$1\E/;
print $s, "\n"; #==> going to capitalize Foo now
|