How to case-insensitively check if a character exists in a string with Pascal

1 Answer

0 votes
program CheckCharIgnoreCase;

{$mode objfpc}

// Convert a character to lowercase safely
function ToLowerChar(c: Char): Char;
begin
  // Wrap c in a string before calling LowerCase
  Result := LowerCase(String(c))[1];
end;

// Case-insensitive check without a loop
function CharExistsIgnoreCase(const s: string; target: Char): Boolean;
begin
  // Convert both to lowercase and use Pos() instead of a loop
  Result := Pos(ToLowerChar(target), LowerCase(s)) > 0;
end;

var
  s: string;
  exists: Boolean;

begin
  // Define the string we want to search in
  s := 'FreePascal';

  // Perform the case-insensitive check
  exists := CharExistsIgnoreCase(s, 'f');

  // Print the raw boolean result
  WriteLn(exists);

  // Conditional check
  if exists then
    WriteLn('exists')
  else
    WriteLn('not exists');
end.



(*
run:

TRUE
exists

*)

 



answered 8 hours ago by avibootz
edited 4 hours ago by avibootz

Related questions

...