Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Proper Case routine

Posted on 2002-03-23
Medium Priority
717 Views
Hello,

Anyone out there have a string routine to convert names to proper case. My database has all customer names in upper case and I wnat to convert them to proper case.

Thanks,
0
Question by:dhertzfe
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 4

LVL 3

Expert Comment

ID: 6891406
try this.
function ToProper(S: String): String;
var
Delimiter: Boolean;
I, L: Integer;
begin
Delimiter := True;
L := Length(S);
for I := 1 to L
do
if not (S[I] in ['a'..'z', 'A'..'Z'])
then Delimiter := True
else
if Delimiter then
begin
if S[I] in ['a'..'z']
then S[I] := Chr(Ord(S[I]) - 32);
Delimiter := False;
end
else
if S[I] in ['A'..'Z']
then S[I] := Chr(Ord(S[I]) + 32);
Result := S;
end;

Regards,
Steve
0

Author Comment

ID: 6891654
Steve,

THis is pretty good. It even handle O'Doole correctly. If you add Mc and Mac like McDonald and MacAuthur I'll raise the points another 100 and award them to you.

Thanks,
0

LVL 3

Expert Comment

ID: 6891795
If you can wait, i've nearly done finished but got to go now. you hand it a TStrings with whatever 'de','la','mc','mac' etc. you need
so locking question till then.
back soon
0

LVL 1

Expert Comment

ID: 6892058

What Steve is proposing is good. However, I have a comment. I have built case converters for professional products and in every case the robustness of the solution can only be solved by incorporating an exception dictionary. You will be amazed at how people's names don't follow the rules.

Roger
0

LVL 3

Expert Comment

ID: 6892963
i'm just testing... seems ok!
If you reject the proposed answer then and raise the points... :)

Steve
0

Author Comment

ID: 6894210
Need to....Steve is sending his code and need to raise the points.

0

Author Comment

ID: 6894219
Hi Roger,

Thanks for the heads up.....I will keep that in mind. Its an excellent idea.

0

LVL 3

Accepted Solution

SteveWaite earned 800 total points
ID: 6894253
I think this is as far as you can go with this method, as Roger correctly points out.
// Capitalise Names
// 'maclintock' = 'MacLintock' :)
// 'macclesfield' = 'MacClesfield' :(
function ToProperName(S: String): String;
const
CaseDiff = 32;
Crlf = #13#10;
Names = 'mc' + Crlf + 'mac';
var
Delimiter: Boolean;
I, J, K, L, M: Integer;
NamesList: TStringList;
begin
NamesList := TStringList.Create;
try
NamesList.Text := Names;
K := NamesList.Count - 1;
L := Length(S);
Delimiter := True;
for I := 1 to L do
begin
if S[I] in ['a'..'z', 'A'..'Z'] then
begin
if Delimiter then
begin
if S[I] in ['a'..'z']
then S[I] := Chr(Ord(S[I]) - CaseDiff);
Delimiter := False;
end
else
begin
if S[I] in ['A'..'Z']
then S[I] := Chr(Ord(S[I]) + CaseDiff);
for J := 0 to K do
begin
M := Length(NamesList.Strings[J]);
if (I = M) then
if ToLowerCase(NamesList.Strings[J])
= ToLowerCase(Copy(S, I - (M - 1), M)) then
begin
Delimiter := True;
Break;
end;
end;
end;
end
else Delimiter := True;
end;
finally
NamesList.Free;
end;
Result := S;
end;

Regards,
Steve
0

LVL 3

Expert Comment

ID: 6894281
need this too...
function ToLowerCase(S: String): String;
var
I, L: Integer;
begin
L := Length(S);
for I := 1 to L
do S[I] := Chr(Ord(S[I]) or \$20);
Result := S;
end;
0

Author Comment

ID: 6904496
Thanks Steve,

Sorry for getting back so late...I guess I wear too many hats.

Thanks again,
0

LVL 3

Expert Comment

ID: 6906284

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small printâ€¦
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have toâ€¦
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonstâ€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
###### Suggested Courses
Course of the Month10 days, 1 hour left to enroll