CharacterTypes (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example demostrates the use of some routines in Character unit 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 IsDigit(AllText[I]) then Inc(LDigits);

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

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

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

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

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

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

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

    { Check for symbols }
    if 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

See Also