Export (0) Print
Expand All

CompareInfo.Compare Method

Compares two strings.

Overload List

Compares two strings using the default CompareOptions value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Compare(String, String) As Integer
[C#] public virtual int Compare(string, string);
[C++] public: virtual int Compare(String*, String*);
[JScript] public function Compare(String, String) : int;

Compares two strings using the specified CompareOptions value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Compare(String, String, CompareOptions) As Integer
[C#] public virtual int Compare(string, string, CompareOptions);
[C++] public: virtual int Compare(String*, String*, CompareOptions);
[JScript] public function Compare(String, String, CompareOptions) : int;

Compares the end section of a string with the end section of another string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, String, Integer) As Integer
[C#] public virtual int Compare(string, int, string, int);
[C++] public: virtual int Compare(String*, int, String*, int);
[JScript] public function Compare(String, int, String, int) : int;

Compares the end section of a string with the end section of another string using the specified CompareOptions value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, String, Integer, CompareOptions) As Integer
[C#] public virtual int Compare(string, int, string, int, CompareOptions);
[C++] public: virtual int Compare(String*, int, String*, int, CompareOptions);
[JScript] public function Compare(String, int, String, int, CompareOptions) : int;

Compares a section of one string with a section of another string.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, Integer, String, Integer, Integer) As Integer
[C#] public virtual int Compare(string, int, int, string, int, int);
[C++] public: virtual int Compare(String*, int, int, String*, int, int);
[JScript] public function Compare(String, int, int, String, int, int) : int;

Compares a section of one string with a section of another string using the specified CompareOptions value.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Overridable Function Compare(String, Integer, Integer, String, Integer, Integer, CompareOptions) As Integer
[C#] public virtual int Compare(string, int, int, string, int, int, CompareOptions);
[C++] public: virtual int Compare(String*, int, int, String*, int, int, CompareOptions);
[JScript] public function Compare(String, int, int, String, int, int, CompareOptions) : int;

Example

[Visual Basic, C#, C++] The following code example compares portions of two strings using different CompareOptions settings.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Compare. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "My Uncle Bill's clients"
      Dim myStr2 As [String] = "My uncle bills clients"

      ' Creates a CompareInfo that uses the InvariantCulture.
      Dim myComp As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myComp.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(3, 10), myStr2.Substring(3, 10))
      Console.WriteLine("   With no CompareOptions            : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10))
      Console.WriteLine("   With None                         : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None))
      Console.WriteLine("   With Ordinal                      : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal))
      Console.WriteLine("   With StringSort                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort))
      Console.WriteLine("   With IgnoreCase                   : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase))
      Console.WriteLine("   With IgnoreSymbols                : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols))
      Console.WriteLine("   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase Or CompareOptions.IgnoreSymbols))

   End Sub 'Main 

End Class 'SamplesCompareInfo


'This code produces the following output.
'
'Comparing "Uncle Bill" and "uncle bill"
'   With no CompareOptions            : 1
'   With None                         : 1
'   With Ordinal                      : -32
'   With StringSort                   : 1
'   With IgnoreCase                   : 0
'   With IgnoreSymbols                : 1
'   With IgnoreCase and IgnoreSymbols : 0


[C#] 
using System;
using System.Globalization;

public class SamplesCompareInfo  {

   public static void Main()  {

      // Defines the strings to compare.
      String myStr1 = "My Uncle Bill's clients";
      String myStr2 = "My uncle bills clients";

      // Creates a CompareInfo that uses the InvariantCulture.
      CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

      // Compares two strings using myComp.
      Console.WriteLine( "Comparing \"{0}\" and \"{1}\"", myStr1.Substring( 3, 10 ), myStr2.Substring( 3, 10 ) );
      Console.WriteLine( "   With no CompareOptions            : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10 ) );
      Console.WriteLine( "   With None                         : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.None ) );
      Console.WriteLine( "   With Ordinal                      : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.Ordinal ) );
      Console.WriteLine( "   With StringSort                   : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.StringSort ) );
      Console.WriteLine( "   With IgnoreCase                   : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase ) );
      Console.WriteLine( "   With IgnoreSymbols                : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreSymbols ) );
      Console.WriteLine( "   With IgnoreCase and IgnoreSymbols : {0}", myComp.Compare( myStr1, 3, 10, myStr2, 3, 10, CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols ) );

   }

}


/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main() {
   // Defines the strings to compare.
   String*  myStr1 = S"My Uncle Bill's clients";
   String*  myStr2 = S"My uncle bills clients";

   // Creates a CompareInfo that uses the InvariantCulture.
   CompareInfo*  myComp = CultureInfo::InvariantCulture->CompareInfo;

   // Compares two strings using myComp.
   Console::WriteLine(S"Comparing \"{0}\" and \"{1}\"", myStr1->Substring(3, 10), 
      myStr2->Substring(3, 10));
      Console::WriteLine(S"   With no CompareOptions            : {0}", 
         __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10)));
   Console::WriteLine(S"   With None                         : {0}", 
      __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions::None)));
   Console::WriteLine(S"   With Ordinal                      : {0}", 
      __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions::Ordinal)));
   Console::WriteLine(S"   With StringSort                   : {0}", 
      __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions::StringSort)));
   Console::WriteLine(S"   With IgnoreCase                   : {0}", 
      __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions::IgnoreCase)));
   Console::WriteLine(S"   With IgnoreSymbols                : {0}", 
      __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10, CompareOptions::IgnoreSymbols)));
   Console::WriteLine(S"   With IgnoreCase and IgnoreSymbols : {0}", 
      __box(myComp->Compare(myStr1, 3, 10, myStr2, 3, 10,static_cast<CompareOptions>(CompareOptions::IgnoreCase | CompareOptions::IgnoreSymbols))));
}

/*
This code produces the following output.

Comparing "Uncle Bill" and "uncle bill"
   With no CompareOptions            : 1
   With None                         : 1
   With Ordinal                      : -32
   With StringSort                   : 1
   With IgnoreCase                   : 0
   With IgnoreSymbols                : 1
   With IgnoreCase and IgnoreSymbols : 0

*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

CompareInfo Class | CompareInfo Members | System.Globalization Namespace

Show:
© 2014 Microsoft