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

Delphi String Case Conversion

By on in Coding

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

Have a question or suggestion? Please leave a comment to start the discussion.

 

Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.