Web Design that taps into the haromny and vision of your dreams.

Delphi String Case Conversion

Sentence case and title case

Written By on in Software Engineering

152 words, estimated reading time 1 minutes.

These procedures will convert the case of a string to Title case, Sentence case or toggle the existing case (convert upper to lower and lower to upper case)

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;

Last updated on: Saturday 24th June 2017

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me