RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrComp Function

Compares two strings with case sensitivity.

Pascal
function StrComp(const Str1: PAnsiChar; const Str2: PAnsiChar): Integer; overload;
function StrComp(const Str1: PWideChar; const Str2: PWideChar): Integer; overload;
C++
int StrComp(const PAnsiChar Str1, const PAnsiChar Str2);
int StrComp(const PWideChar Str1, const PWideChar Str2);

Call StrComp to compare two null-terminated strings, with case sensitivity. The return value is indicated in the following table:

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

Note: When working with International characters, use AnsiStrComp instead.
 

C++ Examples: 

 

/*
The following example uses two edit controls and a button on
a form. When the button is clicked, the text in the edit
controls is compared.
*/

#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() + 20]);
  int iResult = StrComp(Edit1->Text.t_str(), Edit2->Text.t_str());
  StrCopy(szResult.get(), Edit1->Text.t_str());
  if (iResult < 0)
    StrCat(szResult.get(), " is less than ");
  else if (iResult > 0)
    StrCat(szResult.get(), " is greater than ");
  else
    StrCat(szResult.get(), " is equal to ");

  StrCat(szResult.get(), AnsiString(Edit2->Text).c_str());
  ShowMessage(AnsiString(szResult.get()));
}

 

Delphi Examples: 

{
The following example uses two edit controls and a button on
a form. When the button is clicked, the text in the edit
controls is compared.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  Msg: string;
  CompResult: Integer;
begin
  Msg := Edit1.Text;
  CompResult := SysUtils.StrComp(PChar(Edit1.Text), PChar(Edit2.Text));
  if CompResult < 0 then
    Msg := Msg + ' is less than '
  else if CompResult > 0 then
    Msg := Msg + ' is greater than '
  else
    Msg := Msg + ' is equal to ';
  Msg := Msg + Edit2.Text;
  ShowMessage(Msg);
end;

 

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