C# String Compare(String, String, CultureInfo, CompareOptions)

Description

String Compare(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.

Syntax

String.Compare(String, String, CultureInfo, CompareOptions) has the following syntax.


public static int Compare(
  string strA,/* w ww.j  a  v a 2 s .  c  o m*/
  string strB,
  CultureInfo culture,
  CompareOptions options
)

Parameters

String.Compare(String, String, CultureInfo, CompareOptions) has the following parameters.

  • strA - The first string to compare.
  • strB - The second string to compare.
  • culture - The culture that supplies culture-specific comparison information.
  • options - Options to use when performing the comparison (such as ignoring case or symbols).

Returns

String.Compare(String, String, CultureInfo, CompareOptions) method returns 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 is less than strB. Zero strA equals strB. Greater than zero strA is greater than strB.

Example

The following example shows how to use String.Compare(String, String, CultureInfo, CompareOptions) method.


using System;/*from w ww .j  av a  2s. c  o  m*/
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);
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version