Home  |  Delphi .net Home  |  System.String  |  Compare Method
Compare  
Method  
Compares two specified String objects.
String Class
System NameSpace
CF1.  Function Compare ( ValueA:StringValueA : String; ValueB : String; ) : Integer;
CF2.  Function Compare ( ValueA:StringValueA : String; ValueB : String; IgnoreCase : Boolean; ) : Integer;
CF3.  Function Compare ( ValueA:StringValueA : String; ValueB : String; IgnoreCase : Boolean; Culture : CultureInfo; ) : Integer;
CF4.  Function Compare ( ValueA:StringValueA : String; StartA : Integer; ValueB : String; StartB : Integer; Length : Integer; ) : Integer;
CF5.  Function Compare ( ValueA:StringValueA : String; StartA : Integer; ValueB : String; StartB : Integer; Length : Integer; IgnoreCase : Boolean; ) : Integer;
CF6.  Function Compare ( ValueA:StringValueA : String; StartA : Integer; ValueB : String; StartB : Integer; Length : Integer; IgnoreCase : Boolean; Culture : CultureInfo; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns one of the following values :
 
ValueA < ValueB Gives <0
ValueA = ValueB Gives ?0
ValueA > ValueB Gives >0

 
The exact value is not guaranteed.
 
The comparison is case sensitive unless IgnoreCase is true.
 
Lower case letters are deemed to be lower in value than upper case letters.
 
The comparison is for the whole of each string unless the StartA, StartB string index positions and Length values are specified.
Notes
Very Important : Methods in .Net treat strings as starting at 0, unlike traditional Delphi where they started at 1.

Static methods are not methods of an object - they are simply class functions or procedures available at any time.

They may apply to objects, but the objects are always provided as parameters.
Microsoft MSDN Links
System
System.String
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

var
  strA, strB : String;
  result        : Integer;

begin
  strA := 'ABC';
  strB := 'DEF';

  result := System.String.Compare(strA, strB);
  Console.WriteLine(strA + ' compared to ' + strB + ' = ' + result.ToString);

  result := System.String.Compare(strA, strA);
  Console.WriteLine(strA + ' compared to ' + strA + ' = ' + result.ToString);

  result := System.String.Compare(strB, strA);
  Console.WriteLine(strB + ' compared to ' + strA + ' = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  ABC compared to DEF = -1
  ABC compared to ABC = 0
  DEF compared to ABC = 1
Showing case sensitivity
program Project1;
{$APPTYPE CONSOLE}

var
  strA, strB : String;
  result        : Integer;

begin
  strA := 'ABC';
  strB := 'abc';

  Console.WriteLine('Comparing ABC to abc');

  // Default operation (case sensitive)
  result := System.String.Compare(strA, strB);
  Console.WriteLine('Default Compare     = ' + result.ToString);

  // Force case sensitivity
  result := System.String.Compare(strA, strB, false);
  Console.WriteLine('IgnoreCase is False = ' + result.ToString);

  // Ignore case
  result := System.String.Compare(strA, strB, true);
  Console.WriteLine('IgnoreCase is True  = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  Comparing ABC to abc
  Default Operation   = 1
  IgnoreCase is False = 1
  IgnoreCase is True  = 0
Sub-String comparison
program Project1;
{$APPTYPE CONSOLE}

var
  strA, strB : String;
  result     : Integer;

begin
  strA := 'FIRST';
  strB := 'THIRSTY';

  Console.WriteLine('strA                   = ' + strA);
  Console.WriteLine('SubString(1,4) of strA =  ' + strA.Substring(1,4));
  Console.WriteLine('strB                   = ' + strB);
  Console.WriteLine('SubString(2,4) of strB =   ' + strB.Substring(2,4));

  result := System.String.Compare(strA, strB);
  Console.WriteLine('Full String Compare    = ' + result.ToString);

  result := System.String.Compare(strA, 1, strB, 2, 4);
  Console.WriteLine('Sub-string compare     = ' + result.ToString);

  Console.ReadLine;
end.
Show full unit code
  strA                   = FIRST
  SubString(1,4) of strA =  IRST
  strB                   = THIRSTY
  SubString(2,4) of strB =   IRST
  Full String Compare    = -1
  Sub-string compare     = 0
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author