C# String Compare(String, String, StringComparison)

Description

String Compare(String, String, StringComparison) compares two specified String objects using the specified rules, and returns an integer that indicates their relative position in the sort order.

Syntax

String.Compare(String, String, StringComparison) has the following syntax.


public static int Compare(
  string strA,//www .  j a  v a2s.c  om
  string strB,
  StringComparison comparisonType
)

Parameters

String.Compare(String, String, StringComparison) has the following parameters.

  • strA - The first string to compare.
  • strB - The second string to compare.
  • comparisonType - One of the enumeration values that specifies the rules to use in the comparison.

Returns

String.Compare(String, String, StringComparison) method returns A 32-bit signed integer that indicates the lexical relationship between the two comparands. Value Condition Less than zero strA is less than strB. Zero strA equals strB. Greater than zero strA is greater than strB.

Example

This example demonstrates the System.String.Compare(String, String, StringComparison) method.


/*from  ww w.  j a  v  a 2  s  .  com*/
using System;

class Sample 
{
    public static void Main() 
    {

      StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

      int cmpValue = String.Compare("asdf", "Asdf", StringComparison.CurrentCulture);
    
      if(cmpValue < 0) 
         Console.WriteLine( "less than");
      else if (cmpValue > 0)
         Console.WriteLine("greater than");
    }
}

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