How do I Capitalize the first letter?

N

nntp

s/\s(\w)//g;
how to inclue the fist letter in too?

I want to make
abc xyz to Abc Xyz
 
P

Paul Lalli

nntp said:
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
 
B

Ben Morrow

Quoth Paul Lalli said:
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
 
J

John W. Krahn

l said:
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
 
A

Anno Siegel

Paul Lalli said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top