Capitalize Words in a String 
Author Message
 Capitalize Words in a String

I finally did a (crude) subroutine that met my needs, but I spent half
the day trying to figure out how to capitalize all words in a string
using substitutions (i.e. $somestring =~ s/blah/someotherblah/).

Anyone out there been there done that?
  === Al



Mon, 12 Jan 1998 03:00:00 GMT  
 Capitalize Words in a String

Quote:
>I finally did a (crude) subroutine that met my needs, but I spent half
>the day trying to figure out how to capitalize all words in a string
>using substitutions (i.e. $somestring =~ s/blah/someotherblah/).

Couldn't you use the substitution capabilities?:

e.g. $string = ~ s/a-z/A-Z/g ..i believe this will substitute all lowercase
letters to uppercase. the tilde _is_ needed, and the g simply says try to make
as many global changes as possible.

==============================================================================

SysAdmin/Tech Webmaster/Programming                     Knowledge Base Project
                http://www.hitl.washington.edu/people/csmith
==============================================================================



Tue, 13 Jan 1998 03:00:00 GMT  
 Capitalize Words in a String

Quote:


> >I finally did a (crude) subroutine that met my needs, but I spent half
> >the day trying to figure out how to capitalize all words in a string
> >using substitutions (i.e. $somestring =~ s/blah/someotherblah/).
> e.g. $string = ~ s/a-z/A-Z/g ..i believe this will substitute all lowercase
> letters to uppercase. the tilde _is_ needed, and the g simply says try to make
> as many global changes as possible.

only if you want to change the substring "a-z" to "A-Z".  it's always nice
to try these things before posting them (who said "PERL is an empirical
science"?)

how about:

*****************************
#!/usr/bin/perl
$_ = "this is a string\n";

s/\b(\w)/\U\1/g;

print;
*****************************

with output:

This Is A String

if you just want to capitalized everything indiscriminately, look into the
transliteration operater, tr///;

*****************************
#!/usr/bin/perl
$_ = "this is a string\n";

tr/a-z/A-Z/;

print;
*****************************

with output

THIS IS A STRING



Tue, 13 Jan 1998 03:00:00 GMT  
 Capitalize Words in a String

Quote:
> >the day trying to figure out how to capitalize all words in a string
> >using substitutions (i.e. $somestring =~ s/blah/someotherblah/).
> Couldn't you use the substitution capabilities?:
> e.g. $string = ~ s/a-z/A-Z/g ..i believe this will substitute all lowercase
> letters to uppercase. the tilde _is_ needed, and the g simply says try to make
> as many global changes as possible.

I missed the original post, but this wont work. Use:

$string =~ tr/a-z/A-Z/;

--
Lars Marowsky-Bree   Voice: +49-571-63663   PGP-key avail. via server

PGP-Id: 0xDAB57941 / CF FC 3A F0 86 F1 D3 EB  79 8A CF 75 4F 4C 81 DF

Quote:
>USER,n.: The word computer professionals use when they mean 'idiot'<



Thu, 15 Jan 1998 03:00:00 GMT  
 Capitalize Words in a String

Quote:

>I finally did a (crude) subroutine that met my needs, but I spent half
>the day trying to figure out how to capitalize all words in a string
>using substitutions (i.e. $somestring =~ s/blah/someotherblah/).
>Anyone out there been there done that?
>  === Al

Look up 'tr' function


Sun, 18 Jan 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. How to Capitalize the first letter of a word an or whole word

2. Capitalizing words?

3. how to capitalize words

4. Capitalizing words in a line.

5. Capitalizing words...

6. Capitalizing a word (ie tr xxxxxx to Xxxxxx) ???

7. Capitalizing a string with Regexp?

8. Capitalize the first letter of a string?

9. RegEx: Capitalize 1st char. of string with spaces

10. Capitalizing a string

11. Capitalizing First Letters of a String

12. Capitalizing parts of a string

 

 
Powered by phpBB® Forum Software