Website development and design blog, tutorials and inspiration

Delphi String Case Conversion

Sentence case and title case

Written By on in Software Engineering 0

Delphi String Case Conversion

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)

  1. function SentenceCase(Text2:string):string;
  2. var i:integer;
  3. t: string;
  4. begin
  5. for i:=2 to Length(Text2) do
  6. begin
  7. if (Text2[i-1] in ['.','!','?']) then
  8. begin
  9. Text2[i]:=UpCase(Text2[i]);
  10. end
  11. else
  12. begin
  13. t := LowerCase(Text2[i]);
  14. Text2[i]:=t[1];
  15. end;
  16. end;
  17. Result:=Text2;
  18. end;
  19.  
  20. function ToggleCase(Text2:string):string;
  21. var i:integer;
  22. t: string;
  23. begin
  24. for i:=2 to length(Text2) do
  25. begin
  26. if (Text2[i] in ['A'..'Z']) then
  27. begin
  28. t := LowerCase(Text2[i]);
  29. Text2[i]:= t[1];
  30. end
  31. else
  32. if (Text2[i] in ['a'..'z']) then
  33. begin
  34. Text2[i]:=UpCase(Text2[i])
  35. end;
  36. end;
  37. Result:=Text2;
  38. end;
  39.  
  40. function TitleCase(Text2:string):string;
  41. var i:integer;
  42. t: string;
  43. begin
  44. for i:=2 to length(Text2) do
  45. begin
  46. if (not(Text2[i-1] in ['A'..'Z','a'..'z'])) then
  47. Text2[i]:=UpCase(Text2[i])
  48. else
  49. begin
  50. t := LowerCase(Text2[i]);
  51. Text2[i] := t[1];
  52. end;
  53. end;
  54. Result:=Text2;
  55. end;

Last updated on: Saturday 24th June 2017

Did you Like this Post? Why not Like us on Facebook?

 

Comments

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

Leave a Reply

Your email address will not be published.