Delphi String Case Conversion - Sentence Case, Title Case, Toggle

Delphi String Case Conversion functions for Title Case, Sentence case or toggle the case (convert upper to lower, lower to upper case)

By Tim TrottLegacy Code • February 6, 2004
pascal
function SentenceCase(Text2:string):string;
var i:integer;
    t: string;
begin
   for i:=2 to Length(Text2) do
   begin
     if (Text2[i-1] in ['.','!','?']) then
     begin
       Text2[i]:=UpCase(Text2[i]);
     end
     else
     begin
       t := LowerCase(Text2[i]);
       Text2[i]:=t[1];
     end;
   end;
   Result:=Text2;
end;

function ToggleCase(Text2:string):string;
var i:integer;
    t: string;
begin
   for i:=2 to length(Text2) do
   begin
     if (Text2[i] in ['A'..'Z']) then
     begin
       t := LowerCase(Text2[i]);
       Text2[i]:= t[1];
     end
     else
     if (Text2[i] in ['a'..'z']) then
     begin
       Text2[i]:=UpCase(Text2[i])
     end;
   end;
   Result:=Text2;
end;

function TitleCase(Text2:string):string;
var i:integer;
    t: string;
begin
   for i:=2 to length(Text2) do
   begin
     if (not(Text2[i-1] in ['A'..'Z','a'..'z'])) then
      Text2[i]:=UpCase(Text2[i])
     else
     begin
       t := LowerCase(Text2[i]);
       Text2[i] := t[1];
     end;
   end;
   Result:=Text2;
end;

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.