.NET Framework Class Library
StringCompare Method (String, String, CultureInfo, CompareOptions)

Compares two specified String objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two strings to each other in the sort order.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function Compare ( _
	strA As [%$TOPIC/cc190529_en-us_VS_110_2_0_0_0_0%], _
	strB As [%$TOPIC/cc190529_en-us_VS_110_2_0_0_0_1%], _
	culture As [%$TOPIC/cc190529_en-us_VS_110_2_0_0_0_2%], _
	options As [%$TOPIC/cc190529_en-us_VS_110_2_0_0_0_3%] _
) As [%$TOPIC/cc190529_en-us_VS_110_2_0_0_0_4%]
public static [%$TOPIC/cc190529_en-us_VS_110_2_0_1_0_0%] Compare(
	[%$TOPIC/cc190529_en-us_VS_110_2_0_1_0_1%] strA,
	[%$TOPIC/cc190529_en-us_VS_110_2_0_1_0_2%] strB,
	[%$TOPIC/cc190529_en-us_VS_110_2_0_1_0_3%] culture,
	[%$TOPIC/cc190529_en-us_VS_110_2_0_1_0_4%] options
)
public:
static [%$TOPIC/cc190529_en-us_VS_110_2_0_2_0_0%] Compare(
	[%$TOPIC/cc190529_en-us_VS_110_2_0_2_0_1%]^ strA, 
	[%$TOPIC/cc190529_en-us_VS_110_2_0_2_0_2%]^ strB, 
	[%$TOPIC/cc190529_en-us_VS_110_2_0_2_0_3%]^ culture, 
	[%$TOPIC/cc190529_en-us_VS_110_2_0_2_0_4%] options
)
static member Compare : 
        strA:[%$TOPIC/cc190529_en-us_VS_110_2_0_3_0_0%] * 
        strB:[%$TOPIC/cc190529_en-us_VS_110_2_0_3_0_1%] * 
        culture:[%$TOPIC/cc190529_en-us_VS_110_2_0_3_0_2%] * 
        options:[%$TOPIC/cc190529_en-us_VS_110_2_0_3_0_3%] -> [%$TOPIC/cc190529_en-us_VS_110_2_0_3_0_4%]
public static function Compare(
	strA : [%$TOPIC/cc190529_en-us_VS_110_2_0_4_0_0%], 
	strB : [%$TOPIC/cc190529_en-us_VS_110_2_0_4_0_1%], 
	culture : [%$TOPIC/cc190529_en-us_VS_110_2_0_4_0_2%], 
	options : [%$TOPIC/cc190529_en-us_VS_110_2_0_4_0_3%]
) : [%$TOPIC/cc190529_en-us_VS_110_2_0_4_0_4%]

Parameters

strA
Type: SystemString

The first string to compare.

strB
Type: SystemString

The second string to compare.

culture
Type: System.GlobalizationCultureInfo

The culture that supplies culture-specific comparison information.

options
Type: System.GlobalizationCompareOptions

Options to use when performing the comparison (such as ignoring case or symbols).

Return Value

Type: SystemInt32
A 32-bit signed integer that indicates the lexical relationship between strA and strB, as shown in the following table

Value

Condition

Less than zero

strA precedes strB in the sort order.

Zero

strA occurs in the same position as strB in the sort order.

Greater than zero

strA follows strB in the sort order.

Exceptions
ExceptionCondition
ArgumentException

options is not a CompareOptions value.

ArgumentNullException

culture is .

Remarks

The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.

Caution noteCaution

The Compare(String, String, CultureInfo, CompareOptions) method is designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent (that is, when the purpose of the method call is to test for a return value of zero). To determine whether two strings are equivalent, call the Equals method.

The comparison can be further specified by the options parameter, which consists of one or more members of the CompareOptions enumeration. However, because the purpose of this method is to conduct a culture-sensitive string comparison, the CompareOptionsOrdinal and CompareOptionsOrdinalIgnoreCase values have no effect.

Either or both comparands can be . By definition, any string, including StringEmpty, compares greater than a null reference, and two null references compare equal to each other.

The comparison terminates when an inequality is discovered or both strings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, the string with the remaining characters is considered greater.

Notes to Callers

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. The Compare(String, String, CultureInfo, CompareOptions) method does not consider such characters when it performs a culture-sensitive comparison. To recognize ignorable characters in your comparison, supply a value of CompareOptionsOrdinal or CompareOptionsOrdinalIgnoreCase for the options parameter.

Examples

The following example compares two strings in three different ways: using linguistic comparison for the en-US culture; using linguistic case-sensitive comparison for the en-US culture; and using an ordinal comparison. It illustrates how the three methods of comparison produce three different results.

Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim string1 As String = "brother" 
      Dim string2 As String = "Brother" 
      Dim relation As String 
      Dim result As Integer 

      ' Cultural (linguistic) comparison.
      result = String.Compare(string1, string2, _
                              New CultureInfo("en-US"), CompareOptions.None)
      If result > 0 Then
         relation = "comes after" 
      ElseIf result = 0 Then
         relation = "is the same as" 
      Else
         relation = "comes before" 
      End If
      Console.WriteLine("'{0}' {1} '{2}'.", string1, relation, string2)

      ' Cultural (linguistic) case-insensitive comparison.
      result = String.Compare(string1, string2, _
                              New CultureInfo("en-US"), CompareOptions.IgnoreCase)
      If result > 0 Then
         relation = "comes after" 
      ElseIf result = 0 Then
         relation = "is the same as" 
      Else
         relation = "comes before" 
      End If
      Console.WriteLine("'{0}' {1} '{2}'.", string1, relation, string2)

      ' Culture-insensitive ordinal comparison.
      result = String.CompareOrdinal(string1, string2)
      If result > 0 Then
         relation = "comes after" 
      ElseIf result = 0 Then
         relation = "is the same as" 
      Else
         relation = "comes before" 
      End If
      Console.WriteLine("'{0}' {1} '{2}'.", string1, relation, string2)
   End Sub 
End Module 
' The example produces the following output: 
'    'brother' comes before 'Brother'.    
'    'brother' is the same as 'Brother'. 
'    'brother' comes after 'Brother'.
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string string1 = "brother";
      string string2 = "Brother";
      string relation;
      int result;

      // Cultural (linguistic) comparison.
      result = String.Compare(string1, string2, new CultureInfo("en-US"), 
                              CompareOptions.None);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console.WriteLine("'{0}' {1} '{2}'.", 
                        string1, relation, string2);

      // Cultural (linguistic) case-insensitive comparison.
      result = String.Compare(string1, string2, new CultureInfo("en-US"), 
                              CompareOptions.IgnoreCase);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console.WriteLine("'{0}' {1} '{2}'.", 
                        string1, relation, string2);

       // Culture-insensitive ordinal comparison.
      result = String.CompareOrdinal(string1, string2);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console.WriteLine("'{0}' {1} '{2}'.", 
                        string1, relation, string2);
   }
}
// The example produces the following output: 
//    'brother' comes before 'Brother'.    
//    'brother' is the same as 'Brother'. 
//    'brother' comes after 'Brother'.
using namespace System;
using namespace System::Globalization;

public ref class Example
{
public:
   static void Main()
   {
      String^ string1 = "brother";
      String^ string2 = "Brother";
      String^ relation;
      int result;

      // Cultural (linguistic) comparison.
      result = String::Compare(string1, string2, gcnew CultureInfo("en-US"),
                              CompareOptions::None);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console::WriteLine("'{0}' {1} '{2}'.",
                        string1, relation, string2);

      // Cultural (linguistic) case-insensitive comparison.
      result = String::Compare(string1, string2, gcnew CultureInfo("en-US"),
                              CompareOptions::IgnoreCase);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console::WriteLine("'{0}' {1} '{2}'.",
                        string1, relation, string2);

       // Culture-insensitive ordinal comparison.
      result = String::CompareOrdinal(string1, string2);
      if (result > 0)
         relation = "comes after";
      else if (result == 0)
         relation = "is the same as";
      else
         relation = "comes before";

      Console::WriteLine("'{0}' {1} '{2}'.",
                        string1, relation, string2);
   }
};

int main()
{
    Example::Main();
}


// The example produces the following output: 
//    'brother' comes before 'Brother'. 
//    'brother' is the same as 'Brother'. 
//    'brother' comes after 'Brother'.
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5 SP1, 3.0 SP2, 2.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library