How to remove all letters from an alphanumeric string in Pascal

1 Answer

0 votes
program RemoveAlphabeticCharsProgram;

{$mode objfpc}{$H+}

uses
  SysUtils; // CharInSet

{
  Removes all alphabetic characters (A-Z, a-z) from a string.
  Digits and all other non-letter characters are preserved.
}
function RemoveAlphabeticChars(const S: string): string;
var
  C: Char;
begin
  Result := '';

  for C in S do
  begin
    // Keep the character only if it is not a letter
    if not CharInSet(C, ['A'..'Z', 'a'..'z']) then
      Result := Result + C;
  end;
end;

var
  InputStr: string;
begin
  InputStr := 'ABC123xyz-45_Test';

  Writeln('Original: ', InputStr);
  Writeln('Result  : ', RemoveAlphabeticChars(InputStr));
end.
 
 
 
(*
run:
 
Original: ABC123xyz-45_Test
Result  : 123-45_
 
*)

 



answered 1 day ago by avibootz
...