Thanks,
--
Steve Pietrek, Software Engineer
stevep...@nospam.vanguardsolutions.com
(remove "nospam." to email me)
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>...
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.
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)
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
Clever. However, like most other attempts at on-the-fly input verification,
this one is easily defeated by the ever so ingenious user.
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
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'?"
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.
Not all names, just exceptions to the "first character upper, rest
lower" rule. This is surprisingly small, and often used by address
processing software.
>
> 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
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