Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From upper case to lower case 1

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
0
0
US
I have the following string:

RIGHT HERE FOR YOU

If i want to change it to read:

Right Here For You

Is there any easy way to do this? I know i can change the whole string to either uppercase or lowercase, but is there any type of function to produce the above output? Thanks.
 
Hi axman,

$string = 'HELLO THERE';
$string = lc($string)
$string = ucfirst($string);

print "$string\n";

This produces 'Hello there' which is 1/2 of what you want...

To do each word you'll have to break the string up into words with split() and then build the string back up word by word applying the logic above to each word.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Here is a solution:
Code:
#!/usr/bin/perl -w
use strict;

my @upper = qw(THIS IS SOME TEXT);

my @lower;
foreach my $word (@upper)
{
    my $upper = substr($word,0,1);
    my $lower = substr($word,1,length($word)-1);
    my $updated = uc($upper) . lc($lower);
    push @lower, $updated
}

print join (" ", @lower);
I used a lot of variables which can be removed by combining steps but this is basically the idea.
 
Try this. I stuck it in a sub for ease of use.
Code:
$clean_str = cap_str('RIGHT HERE FOR YOU');
print "Capitalized string: $clean_str\n";
exit;

# convert any string to Capitalized Words
sub cap_str {
  my ($str) = @_;
  my $new;
  foreach (split(" ",$str)) {
    $new .= (ucfirst lc $_) . " ";
  }
  chop $new;
  return $new;
}

Cheers!

--G
 
I'm at work and can't test it, but:
Code:
$string = "RIGHT HERE FOR YOU";
$string =~ s/(\w+)/\u\L$1/g;
print $string;

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
Oh nice, Tom. I KNEW there had to be a way to do it in one line, but I just couldn't quite get ther. Might of known a regex would solve it. Cheers. :)

--G
 
Slight variation on Tom's solution:
Code:
my $string = "RIGHT HERE FOR YOU";
$string =~ s/(\w+)/ucfirst(lc($1))/eg;
print $string;
 
TomThumbKP, you are a bastard ;)

6 of my lines in 1 of yours, you deserve a star.
 
Thanks. It's fortuitous that someone asked a question that I happened to have needed in the past.

[blue]"Well, once again my friend, we find that science is a two headed beast. One head is nice, it gives us aspirin and other modern conveniences,...but the other head of science is BAD! Oh, beware the other head of science, Arthur; it bites!!" - The Tick[/blue]
 
*g* very neat Tom..

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top