C# String Compare(String, String, Boolean)

Description

String Compare(String, String, Boolean) compares two specified String objects, ignoring or honoring their case, and returns an integer that indicates their relative position in the sort order.

Syntax

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


public static int Compare(
  string strA,/*from w  ww  . ja va 2  s  . c  o m*/
  string strB,
  bool ignoreCase
)

Parameters

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

  • strA - The first string to compare.
  • strB - The second string to compare.
  • ignoreCase - true to ignore case during the comparison; otherwise, false.

Returns

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

For example, a culture-sensitive, case-insensitive comparison of "animal" with "Ani-mal" (using a soft hyphen, or U+00AD) indicates that the two strings are equivalent, as the following example shows.


using System;//w w w .  j  a  v a 2s  .  c o m

public class Example
{
   public static void Main()
   {
      string s1 = "Ani\u00ADmal";
      string s2 = "animal";

      Console.WriteLine("Comparison of '{0}' and '{1}': {2}", 
                        s1, s2, String.Compare(s1, s2, true));
   }
}

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