DelphiBasics
AnsiCompareText
Function
Compare two strings for equality, ignoring case SysUtils unit
 function AnsiCompareText ( const String1, String2 : string ) : Integer;
Description
The AnsiCompareText function compares String1 and String2 for equality, ignoring case.
 
This is the modern, Locale safe form of CompareText.
 
All Ansi commands support multi-byte and accented characters.
 
It returns these values :
 
String1 < String2  : -ve number
String1 = String2  : 0
String1 > String2  : +ve number

 
The comparison is not affected by length - it is carried out on a letter by letter basis. But a longer string is greater than a shorter, otherwise matching string.
Notes
In Delphi :

Letters > Numbers

Multi-byte character sets are operating system defined. For example, Oriental versions of Windows uses multi-byte characters to support their very large set of primitives ('letters').

Related commands
AnsiCompareStr Compare two strings for equality
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Compare various strings
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the AnsiCompareText command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure CompareStrings(const string1, string2 : string);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

begin
  // Compare two obviously different strings
  CompareStrings('HELLO', 'WORLD');

  // Compare identical strings
  CompareStrings('Hi 2 you', 'Hi 2 you');

  // AnsiCompareText treats upper and lower case identically
  CompareStrings('ABCdef', 'abcDEF');

  // All letters follow numbers in Delphi
  CompareStrings('abc', '123');
end;

// Compare two strings, and show which is bigger than the other
procedure TForm1.CompareStrings(const string1, string2: string);
var
  result : Integer;
begin
  // Compare some strings
  result := AnsiCompareText(string1, string2);

  if result < 0 then ShowMessage(string1+' < '+string2);
  if result = 0 then ShowMessage(string1+' = '+string2);
  if result > 0 then ShowMessage(string1+' > '+string2);
end;
 
end.
Hide full unit code
   HELLO < WORLD
   Hi 2 you = Hi 2 you
   ABCdef = abcDEF
   abc > 123
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page