RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrComp Function

Compares two strings with case sensitivity.

Pascal
function StrComp(const Str1: PChar; const Str2: PChar): Integer;
C++
int StrComp(const const char * Str1, const const char * Str2);

SysUtils

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.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  char* szResult = new char[Edit1->Text.Length() + Edit2->Text.Length() + 20];
  int iResult = StrComp(Edit1->Text.c_str(), Edit2->Text.c_str());
  StrCopy(szResult, Edit1->Text.c_str());
  if (iResult < 0)
    StrCat(szResult, " is less than ");
  else if (iResult > 0)
    StrCat(szResult, " is greater than ");
  else
    StrCat(szResult, " is equal to ");

  StrCat(szResult, Edit2->Text.c_str());
  ShowMessage(AnsiString(szResult));
  delete [] szResult;
}

 

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) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!