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
|
|
|
|