Solved

Proper Case routine

Posted on 2002-03-23
11
696 Views
Last Modified: 2010-04-05
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,
Chad
0
Comment
Question by:dhertzfe
  • 6
  • 4
11 Comments
 
LVL 3

Expert Comment

by:SteveWaite
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

by:dhertzfe
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,
Chad
0
 
LVL 3

Expert Comment

by:SteveWaite
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

by:rfedyk
ID: 6892058
Hi Chad,

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

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

Steve
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

Chad
0
 

Author Comment

by:dhertzfe
ID: 6894219
Hi Roger,

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

Chad
0
 
LVL 3

Accepted Solution

by:
SteveWaite earned 200 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

by:SteveWaite
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

by:dhertzfe
ID: 6904496
Thanks Steve,

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

Thanks again,
Chad
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6906284
Thanks Chad,

Glad to be of help.

 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 89
Communication Between RC4 Delphi <-> PHP 3 100
find a node in VST 2 63
Press three keys together and trigger a function 3 51
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now