Pascal. Types PAnsiChar, PWideChar. String compare functions

Types PAnsiChar, PWideChar. String compare functions


Contents


Search other resources:

1. Function StrComp. Comparison of two strings

The StrComp function implements string comparison. The function declaration for PAnsiChar and PWideChar types is as follows

function StrComp(const Str1, Str2: PAnsiChar): Integer;
function StrComp(const Str1, Str2: PWideChar): Integer;

here

  • Str1, Str2 – compared strings.

The function returns an integer value, which is interpreted as follows:

  • if Str1<Str2, then a negative number is returned;
  • if Str1>Str2, then a positive number is returned;
  • if Str1=Str2 (strings are equivalent), then 0 is returned.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  ps1 : PWideChar;
  ps2 : PWideChar;
  res : Integer;
  s : string;

begin
  // Function StrComp

  // 1. Create original strings
  ps1 := StrNew('1234567');
  ps2 := StrNew('12345678');

  // 2. Display strings
  Writeln('ps1 = ', StrPas(ps1));
  Writeln('ps2 = ', StrPas(ps2));

  // 3. Compare strings - call StrComp
  res := StrComp(ps1, ps2);

  // 4. Process the result
  if res > 0 then
    Writeln('ps1 > ps2')
  else
  if res = 0 then
    Writeln('ps1 = ps2')
  else
    Writeln('ps1 < ps2');

  // 5. Free memory allocated for strings ps1, ps2
  StrDispose(ps1);
  StrDispose(ps2);

  Readln;
end.

Result

ps1 = 1234567
ps2 = 12345678
ps1 < ps2

 

2. Function StrIComp. Comparing strings case-insensitively

The StrIComp function is used to compare two strings in a case-insensitive manner. The function works in the same way as the StrComp function. The function specification for PAnsiChar and PWideChar types is as follows

function StrIComp(const Str1, Str2: PAnsiChar): Integer;
function StrIComp(const Str1, Str2: PWideChar): Integer;

here

  • Str1, Str2 – compared strings.

The function returns a positive value if Str1 > Str2. If Str1 < Str2, then the function returns a negative result. If the strings are equal in lexicographic order, the function returns 0.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  ps1 : PWideChar;
  ps2 : PWideChar;
  res : Integer;

begin
  // Function StrIComp - compare strings case-insensitively

  // 1. Two strings are given
  ps1 := StrNew('ABCD');
  ps2 := StrNew('abcd');

  // 2. Display strings
  Writeln('ps1 = ', StrPas(ps1));
  Writeln('ps2 = ', StrPas(ps2));

  // 3. Compare strings, get the result
  res := StrIComp(ps1, ps2);

  // 4. Display the result
  if res = 0 then Writeln('ps1 = ps2');
  if res > 0 then Writeln('ps1 > ps2');
  if res < 0 then Writeln('ps1 < ps2');

  // 5. Free up memory
  StrDispose(ps1);
  StrDispose(ps2);

  Readln;
end.

Result

ps1 = ABCD
ps2 = abcd
ps1 = ps2

 

3. Function StrLComp. Comparing two strings with a given maximum length

The StrLComp function is used to compare strings. This compares a fixed number of characters specified by the function parameter. The function declaration for PAnsiChar and PWideChar types is as follows

function StrLComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
function StrLComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;

here

  • Str1, Str2 – compared strings;
  • MaxLen – the number of characters that are taken into account when comparing the strings Str1 and Str2. If the length of the strings is greater, the extra characters are not taken into account.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  ps1 : PAnsiChar;
  ps2 : PAnsiChar;
  res : Integer;

begin

  // StrLComp function - compare two strings with given length

  // 1. Two strings are given
  ps1 := 'ABCDEFGH';
  ps2 := 'ABCD';

  // 2. Compare the first 4 characters of strings, get the result
  res := StrLComp(ps1, ps2, 4);

  // 3. Process the result
  if res > 0 then
    Writeln('ps1 > ps2')
  else
  if res < 0 then
    Writeln('ps1 < ps2')
  else
    Writeln('ps1 = ps2');

  Readln;
end.

Result

ps1 = ps2

 

4. Function StrLIComp. Comparing two strings of a given length, case insensitive

The StrLIComp function implements a symbolic comparison of strings with a given maximum length, case insensitive. For PAnsiChar and PWideChar types, the function has the following declarations

function StrLIComp(const Str1, Str2: PAnsiChar; MaxLen: Cardinal): Integer;
function StrLIComp(const Str1, Str2: PWideChar; MaxLen: Cardinal): Integer;

here

  • Str1, Str2 – compared strings;
  • MaxLen – the maximum allowed number of characters to compare.

The function returns a value greater than 0 if the string in the lexicographic order Str1 > Str2. If Str1 < Str2, the function returns a negative value. If the first MaxLen characters of the strings are equivalent, then the function returns 0.

Example.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  ps1 : PWideChar;
  ps2 : PWideChar;
  res : Integer;

begin
  // Function StrLIComp

  // 1. Form strings ps1, ps2
  ps1 := StrNew('ABCDEFGH');
  ps2 := StrNew('abcdef');

  // 2. Compare strings - function StrLIComp
  res := StrLIComp(ps1, ps2, 6); // The first 6 characters are compared

  // 3. Process the result
  if res > 0 then Writeln('ABCDEFG > abcdef');
  if res < 0 then Writeln('ABCDEFG < abcdef');
  if res = 0 then Writeln('ABCDEFG = abcdef'); // +

  // 4. Free memory allocated for strings ps1, ps2
  StrDispose(ps1);
  StrDispose(ps2);

  Readln;
end.

Result

ABCDEFG = abcdef

 


Related topics