String.Compare Method (String, String, CultureInfo, CompareOptions)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
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 one another in the sort order.
Assembly: mscorlib (in mscorlib.dll)
public static int Compare( string strA, string strB, CultureInfo culture, CompareOptions options )
Parameters
- strA
- Type: System.String
The first string.
- strB
- Type: System.String
The second string.
- culture
- Type: System.Globalization.CultureInfo
The culture that supplies culture-specific comparison information.
- options
- Type: System.Globalization.CompareOptions
Options, such as ignoring case or symbols, to use when performing the comparison.
Return Value
Type: System.Int32A 32-bit signed integer that indicates the lexical relationship between strA and strB.
Value | Condition |
|---|---|
Less than zero | strA is less than strB. |
Zero | strA equals strB. |
Greater than zero | strA is greater than strB. |
| Exception | Condition |
|---|---|
| ArgumentException | options is not a CompareOptions value. |
| ArgumentNullException | culture is null. |
The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetic 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: |
|---|
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 CompareOptions.Ordinal and CompareOptions.OrdinalIgnoreCase values have no effect.
Either or both comparands can be null. By definition, any string, including String.Empty, 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.
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.
using System; using System.Globalization; public class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { 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"; outputBlock.Text += String.Format("'{0}' {1} '{2}'.\n", 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"; outputBlock.Text += String.Format("'{0}' {1} '{2}'.\n", 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"; outputBlock.Text += String.Format("'{0}' {1} '{2}'.\n", string1, relation, string2); } } // The example produces the following output: // 'brother' comes before 'Brother'. // 'brother' is the same as 'Brother'. // 'brother' comes after 'Brother'.
Caution: