Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How do I Capitalize the first letter?

Reply
Thread Tools

How do I Capitalize the first letter?

 
 
nntp
Guest
Posts: n/a
 
      10-08-2004
s/\s(\w)//g;
how to inclue the fist letter in too?

I want to make
abc xyz to Abc Xyz


 
Reply With Quote
 
 
 
 
Jim Keenan
Guest
Posts: n/a
 
      10-08-2004
nntp wrote:
> s/\s(\w)//g;
> how to inclue the fist letter in too?
>
> I want to make
> abc xyz to Abc Xyz
>
>

perldoc -f ucfirst
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      10-08-2004
nntp <(E-Mail Removed)> wrote:

> I want to make
> abc xyz to Abc Xyz



s/(\w+)/\u\L$1/g; # untested


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-09-2004
Jim Keenan wrote:

> nntp wrote:
>
>> s/\s(\w)//g;
>> how to inclue the fist letter in too?
>>
>> I want to make
>> abc xyz to Abc Xyz
>>
>>

> perldoc -f ucfirst


Have you yourself read that documentation? It doesn't do what the OP
asked for.

Paul Lalli
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-09-2004
nntp wrote:

> s/\s(\w)//g;

this is elminating the space and word character. That doesn't at all
match your subject.
> how to inclue the fist letter in too?
>
> I want to make
> abc xyz to Abc Xyz
>
>

\s is searching for a space. You just want to find the first character
in each 'word'. Assuming you don't mind Perl's default definition of
'word' characters (which include digits and underscores), you're going
to want something closer to this:

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

read perldoc perlre for info on \b. Read perldoc -f ucfirst for info on \u

HOWEVER, this will not function as you intend for several phrases. One
obvious example is:
"this won't work", which would become "This Won'T Work".

So perhaps you might actually want to look for the space, but also
inclue the very first character in the string:

s/(^|\s)(\w)/$1\u$2/g;

Paul Lalli
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-09-2004

Quoth Paul Lalli <(E-Mail Removed)>:
>
> s/(^|\s)(\w)/$1\u$2/g;


I would use positive look-behind:

s/(?:^|(?<=\s))(\w)/\u$1/g;

just 'cos it's neater. No variable-length lookbehind is annoying, though...

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ (E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      10-10-2004
l v wrote:
> nntp wrote:
>>
>> s/\s(\w)//g;
>> how to inclue the fist letter in too?
>>
>> I want to make
>> abc xyz to Abc Xyz

>
> Not sure on exactly what you are asking for. Below will capitalize the
> first letter of every word in $a.
>
> join ' ', ( map {ucfirst $_ } split /\W/, $a );


You are changing all the \W characters to ' ' which may not be what the OP
wants. You could fix that by:

join '', map ucfirst, split /(\W+)/, $a;


John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      10-11-2004
Paul Lalli <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Jim Keenan wrote:
>
> > nntp wrote:
> >
> >> s/\s(\w)//g;
> >> how to inclue the fist letter in too?
> >>
> >> I want to make
> >> abc xyz to Abc Xyz
> >>
> >>

> > perldoc -f ucfirst

>
> Have you yourself read that documentation? It doesn't do what the OP
> asked for.


It does point to the "\u" escape, which is what the OP wants:

s/\s(\w+)/ \u$1/g;

or better

s/\b(\w+)/\u$1/g;

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to capitalize first word of a string? Rails List Ruby 4 06-05-2009 05:37 PM
CSS: Is text-transform: capitalize; NG on IE 5.x? Curtis HTML 7 01-06-2006 11:58 PM
Capitalize First Letter Exlcuding ( ) and / gil@ambry.com Javascript 3 06-05-2005 03:06 PM
Regex to capitalize first letter of string jay Java 1 02-25-2004 08:41 AM



Advertisments