TCharacterTypes (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example demostrates the use of some methods of TCharacter class that allow to determine the type of a character. When the user types something in the first memo, the second one gets filled with relevant information.

Code

procedure TMainForm.mmTextChange(Sender: TObject);
var
  AllText: String;
  I: Integer;
  LDigits, LLetters, LNumber,
   LLower, LUpper, LPuct,
    LSep, LSymbols, LWhites: Integer;
begin
  { Calculate all all kinds of charcters in the memo }
  AllText := mmText.Text;

  LDigits := 0;
  LLetters := 0;
  LLower := 0;
  LUpper := 0;
  LNumber := 0;
  LPuct := 0;
  LSep := 0;
  LSymbols := 0;
  LWhites := 0;

  for I := 1 to Length(AllText) do
  begin
    { Check for digit }
    if TCharacter.IsDigit(AllText[I]) then Inc(LDigits);

    { Check for number }
    if TCharacter.IsNumber(AllText[I]) then Inc(LNumber);

    { Check for letter }
    if TCharacter.IsLetter(AllText[I]) then Inc(LLetters);

    { Check for lower-cased letter }
    if TCharacter.IsLower(AllText[I]) then Inc(LLower);

    { Check for upper-cased letter }
    if TCharacter.IsUpper(AllText[I]) then Inc(LUpper);

    { Check for punctuation }
    if TCharacter.IsPunctuation(AllText[I]) then Inc(LPuct);

    { Check for separators }
    if TCharacter.IsSeparator(AllText[I]) then Inc(LSep);

    { Check for symbols }
    if TCharacter.IsSymbol(AllText[I]) then Inc(LSymbols);

    { Check for symbols }
    if TCharacter.IsWhiteSpace(AllText[I]) then Inc(LWhites);
  end;

  mmRes.Text := Format('%d digits; %d numbers; %d letters; ' +
    '(%d upper and %d lower); %d puctuation; %d separators; ' +
    '%d symbols; %d whitespaces',
    [LDigits, LNumber, LLetters, LLower, LUpper, LPuct,
     LSep, LSymbols, LWhites]
  );
end;

Uses