Jump to content

[Delphi] Format a string to proper case

- - - - -

  • Please log in to reply
2 replies to this topic

#1
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts

The following function returns the given string formatted in proper or sentence case, i.e. the first letter is upper case and the rest is in lower case.


E.g:

[LIST]

[*]input: "roses are red", output: "Roses are red"

[*]input: "c", output: "C"

[*]input: "roSES ARe RED", output: "Roses are red"

[/LIST]


[CODE]

function ProperCase(const AString: string): string;

begin

  // make all characters into lower case

  Result := LowerCase(AString);


  // if the supplied string is not empty string, then make the first character an upper case

  if Result <> '' then  UpCase(Result[1])

end;



#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Nice bit of code :)
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 763 posts
^ Lol, thank you! Too simple of a code, I think!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users