Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Capitalize only the first letter

2,703 views
Skip to first unread message

Steve Pietrek

unread,
Mar 23, 1998, 3:00:00 AM3/23/98
to

How do I go about only capitalizing the first letter in each word of a
string and the rest of the letters in lowercase?

Thanks,

--
Steve Pietrek, Software Engineer
stevep...@nospam.vanguardsolutions.com
(remove "nospam." to email me)

Rob Gamble

unread,
Mar 24, 1998, 3:00:00 AM3/24/98
to

I'm a VB programmer who likes to stalk Delphi programmers. :) Seriously,
I'm looking for help on a Delphi project but I thought I'd lend a hand if I
could.

Here's how I would do it:


You need to find every word. They will be delimited by certain characters,
like spaces, commas and accentual punctuation like periods and exclamation
points.

With each word, separate the first letter from the rest of the word like so:

In Delphi:

var
sFirstLetter, sRestOfWord, sWholeWord, sNewWord: string;
begin
sWholeWord := 'example';

sFirstLetter := Copy(sWholeWord, 1, 1);
sRestOfWord := Copy(sWholeWord, 2, Length(sWholeWord));

In VB:

Dim sFirstLetter As String
Dim sRestOfWord As String
Dim sWholeWord As String
Dim sNewWord As String

sWholeWord = "example"

sFirstLetter = Left(sWholeWord, 1)
sRestOfWord = Mid(sWholeWord, 2)

Now uppercase the first letter, and lower case the rest of the word like so:

In Delphi:

sFirstLetter := UpperCase(sFirstLetter);
sRestOfWord := LowerCase(sRestOfWord);

In VB:

sFirstLetter = UCase(sFirstLetter)
sRestOfWord = LCase(sRestOfWord)

Now recombine the two for your new improved word!

In Delphi:

sNewWord := sFirstLetter + sRestOfWord;

In VB:

sNewWord = sFirstLetter & sRestOfWord


Many of these steps can be combined, but that serves only to make the code
less readable. To a beginner, solving the problem is MUCH more important
than using clever formatting tricks.

Steve Pietrek wrote in message <6f8d6k$fk...@forums.borland.com>...

Ernie Deel

unread,
Mar 24, 1998, 3:00:00 AM3/24/98
to

Steve Pietrek <stevep...@vanguardsolutions.com> wrote in article

<6f8d6k$fk...@forums.borland.com>...
> How do I go about only capitalizing the first letter in each word of a
> string and the rest of the letters in lowercase?

HyperString (http://www.mindspring.com/~efd/tools.htm) has ProperCase() and
over 250 other string functions; many written in hand optimized assembler for
maximum speed, minimum size. Something for everyone<g>.

--
Ernie Deel, EFD Systems
-----------------------------------------------
Nothing is random, only uncertain.


Peter Below

unread,
Mar 24, 1998, 3:00:00 AM3/24/98
to

> How do I go about only capitalizing the first letter in each word of a
> string and the rest of the letters in lowercase?
>
Steve, try this function:

Function CapitalizeWords( Const S: String ): String;
Var
i: Integer;
lastWasAlphaNum: Boolean;
Begin
Result:= AnsiLowerCase( S );
lastWasAlphaNum := False;
For i := 1 To Length(Result) Do Begin
If IsCharAlphaNumeric(Result[i]) Then Begin
{ We have a letter or number, is it the first or is the character
in front not a letter or number? }
If not lastWasAlphaNum Then Begin
{ Uppercase the character. Using the API function takes
care of international characters, which UpCase does
not handle. If we have a number it will remain unchanged. }
CharUpperBuff( @Result[i], 1 );
lastWasAlphaNum := True;
End; { If }
End { If }
Else
lastWasAlphaNum := False;
End; { For }
End; { CapitalizeWords }

procedure TForm1.Button2Click(Sender: TObject);
begin
edit2.text := CapitalizeWords( edit2.text );
end;

It will not convert a "word" like 123abc to 123Abc, if you want to have it
do that, too, change IsCharAlphaNumeric to IsCharAlpha.

Peter Below (TeamB) 10011...@compuserve.com)


Guy Schamp

unread,
Mar 24, 1998, 3:00:00 AM3/24/98
to

[This followup was posted to borland.public.delphi.objectpascal and a
copy was sent to the cited author.]

In article <6f8d6k$fk...@forums.borland.com>,
stevep...@vanguardsolutions.com says...


> How do I go about only capitalizing the first letter in each word of a
> string and the rest of the letters in lowercase?
>

> Thanks,
>
> --
> Steve Pietrek, Software Engineer
> stevep...@nospam.vanguardsolutions.com
> (remove "nospam." to email me)

Wish I could take credit for it, but here's a code snippet from
"Instant Delphi Programming" by David Jewell (Wrox Press):

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (Edit1.SelStart = 0) or (Edit1.Text [Edit1.SelStart] = ' ') then
Key := UpCase(Key);
end;

--
Guy Schamp
gsc...@netbridge.net

Ernie Deel

unread,
Mar 25, 1998, 3:00:00 AM3/25/98
to

Guy Schamp <gsc...@netbridge.net> wrote in article
<MPG.f82212b4...@news.netbridge.net>...

> procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
> begin
> if (Edit1.SelStart = 0) or (Edit1.Text [Edit1.SelStart] = ' ') then
> Key := UpCase(Key);
> end;

Clever. However, like most other attempts at on-the-fly input verification,
this one is easily defeated by the ever so ingenious user.

David Block

unread,
Mar 25, 1998, 3:00:00 AM3/25/98
to

On 25 Mar 1998 14:13:04 GMT, "Ernie Deel"
<e...@remove.this.mindspring.com> wrote:

Of course all of this is ridiculous. Nobody has even come close to a
proper solution yet. You need some sort of dictionary to do it
correctly. Otherwise, you are likely to upset the McDonalds,
O'Reilly's, O'Donoghue's and others in this world.

TMHO.

David Block
CoStar Corporation

Check out our label printers at www.costar.com


Email: dblock...@costar.com

Wayne Herbert

unread,
Mar 25, 1998, 3:00:00 AM3/25/98
to

Not to mention that if you capitalize all words you end up messing up names like
Hector Manchez del Garcia which is supposed to have a lower case word.

It reminds me of a project in which I attempted to match male and female voters
in each household so that I could mail to "Mr. and Mrs."... ended up sending a
letter to the party chairman, assuming his daughter was his wife! Too many
variables!!!


BTW: You left out Patty O'Furniture.

David Block wrote:

> Of course all of this is ridiculous. Nobody has even come close to a
> proper solution yet. You need some sort of dictionary to do it
> correctly. Otherwise, you are likely to upset the McDonalds,
> O'Reilly's, O'Donoghue's and others in this world.
>
> TMHO.
>
> David Block
> CoStar Corporation
>
> Check out our label printers at www.costar.com
>
> Email: dblock...@costar.com

--
Wayne Herbert
Manager, Computer Products
Key Maps, Inc.
1411 West Alabama
Houston, TX 77006

Vox: 713.522.7949
Fax: 713.521.3202
Email: wher...@rice.edu

"Why is it only drug dealers and software developers call their clients 'users'?"


Ernie Deel

unread,
Mar 25, 1998, 3:00:00 AM3/25/98
to

David Block <dbl...@vdn.com> wrote in article
<3519227e...@news.nothingbutnet.net>...

> Of course all of this is ridiculous. Nobody has even come close to a
> proper solution yet. You need some sort of dictionary to do it
> correctly. Otherwise, you are likely to upset the McDonalds,
> O'Reilly's, O'Donoghue's and others in this world.

A dictionary of all proper names. Interesting idea but I doubt anyone has
enough time to create it ... or a disk drive big enough to hold it.

David Block

unread,
Mar 26, 1998, 3:00:00 AM3/26/98
to

On 25 Mar 1998 22:47:45 GMT, "Ernie Deel"
<e...@remove.this.mindspring.com> wrote:

Not all names, just exceptions to the "first character upper, rest
lower" rule. This is surprisingly small, and often used by address
processing software.

Ken White

unread,
Mar 31, 1998, 3:00:00 AM3/31/98
to

David Block wrote:

>
> Of course all of this is ridiculous. Nobody has even come close to a
> proper solution yet. You need some sort of dictionary to do it
> correctly. Otherwise, you are likely to upset the McDonalds,
> O'Reilly's, O'Donoghue's and others in this world.

Surprisingly, it isn't that difficult. Clipper Functions for
Delphi has a Proper() function that accounts for most common
exceptions to the 'capitalize first letter' rule, and is
easily extensible to support other exceptions.

Ken
---
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc

Tim Sommers

unread,
Mar 31, 1998, 3:00:00 AM3/31/98
to

On Mon, 23 Mar 1998 16:34:15 -0600, "Steve Pietrek"
<stevep...@vanguardsolutions.com> wrote:
>How do I go about only capitalizing the first letter in each word of a
>string and the rest of the letters in lowercase?
>
>Thanks,
>
>--
>Steve Pietrek, Software Engineer
> stevep...@nospam.vanguardsolutions.com
> (remove "nospam." to email me)
>
>

I wrote this one a while back which can take in to account many
exceptions that you may need to account for:

function ProperName(Value: String): String;
var
I: Integer;
begin
Value := LowerCase(Value);
Value[1] := UpCase(Value[1]);
for I := 1 to Length(Value) do
begin
if Value[I] = ' ' then
begin
Value[I + 1] := UpCase(Value[I + 1]);
end; {if}
if Value[I] = '.' then
begin
if Value[I + 1] <> ' ' then
Value[I + 1] := UpCase(Value[I + 1]);
end; {if}
{...and so on, you get the idea...}
end; {for}
Result := Value;
end;

All it does is to look for a space, or whatever character, and UpCase
the next character. Hope this helps...

Tim

0 new messages