Home  |  Delphi .net Home  |  system.String  |  CompareTo Method
CompareTo  
Method  
Compares the current string with another
String Class
system NameSpace
CF1.  Function CompareTo ( Target : String; ) : Integer ;
CF2.  Function CompareTo ( Target : Object; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
Returns one of the following values :
 
Source < Target Gives <0
Source = Target Gives ?0
Source > Target Gives >0

 
The exact value is not guaranteed.
 
The comparison is case sensitive and culture sensitive. Use Compare to perform a case insensitive comparison.
 
Lower case letters are deemed to be lower in value than upper case letters.
 
An empty string always precedes any other string.
 
The 2nd syntax requires the target object to evaluate to a string.
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 := strA.CompareTo(strB);
  Console.WriteLine(strA + ' compared to ' + strB + ' = ' + result.ToString);

  result := strA.CompareTo(strA);
  Console.WriteLine(strA + ' compared to ' + strA + ' = ' + result.ToString);

  result := strB.CompareTo(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
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author