RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrLComp Function

Compares up to a specified maximum number of characters in two strings.

Pascal
function StrLComp(const Str1: PAnsiChar; const Str2: PAnsiChar; MaxLen: Cardinal): Integer; overload;
function StrLComp(const Str1: PWideChar; const Str2: PWideChar; MaxLen: Cardinal): Integer; overload;
C++
int StrLComp(const PAnsiChar Str1, const PAnsiChar Str2, unsigned MaxLen);
int StrLComp(const PWideChar Str1, const PWideChar Str2, unsigned MaxLen);

StrLComp compares Str1 to Str2, up to a maximum length of MaxLen characters. The return value is indicated in the following table:

Return value 
Condition 
<0  
Str1 sorts before Str2  
=0  
Str1 is the same as Str2  
>0  
Str1 sorts after Str2  

Note: When working with international characters, use AnsiStrLComp instead.
 

C++ Examples: 

 

/*
This example requires two text edits and a button.
Enter text in the two text edits and click the button
to compare the strings.
*/
#include <algorithm>  // for the min function
#include <memory>       // for STL auto_ptr class

void __fastcall TForm1::Button1Click(TObject *Sender)
{
  std::auto_ptr<char> szResult(new char[Edit1->Text.Length() + Edit2->Text.Length() + 65]);
  int len = std::min(Edit1->Text.Length(), Edit2->Text.Length());
  int iResult = Sysutils::StrLComp(
    Edit1->Text.c_str(), Edit2->Text.c_str(), len + 1);
  StrCopy(szResult.get(), "The first ");
  StrCat(szResult.get(), IntToStr(len).t_str());
  StrCat(szResult.get(), " characters of ");
  StrCat(szResult.get(), Edit1->Text.t_str());
  if (iResult < 0)
    StrCat(szResult.get(), " are less than the first ");
  else if (iResult > 0)
    StrCat(szResult.get(), " are greater than the first ");
  else
    StrCat(szResult.get(), " are equal to the first ");
  StrCat(szResult.get(), IntToStr(len).t_str());
  StrCat(szResult.get(), " characters of ");
  StrCat(szResult.get(), Edit2->Text.t_str());

  ShowMessage(szResult.get());
}

 

Delphi Examples: 

{
This example requires two text edits and a button.
Enter text in the two text edits and click the button
to compare the strings.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  S1, S2: String;
  Pos: Integer;
  Equal: Boolean;
begin
  Pos := 1;
  Equal := True;
  S1 := Edit1.Text;
  S2 := Edit2.Text;
  Form1.Repaint;
  while (Equal = True) do
  begin
    if StrLComp(PChar(S1), PChar(S2), Pos) = 0 then
      Inc(Pos)
    else
      Equal := False;
  end;
  Canvas.TextOut(10, 10, 'The first ' + IntToStr(Pos - 1) +
    ' characters are equal.');
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!