C# String CompareTo(String)

Description

String CompareTo(String) compares this instance with a specified String object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified String.

Syntax

String.CompareTo(String) has the following syntax.


public int CompareTo(
  string strB
)

Parameters

String.CompareTo(String) has the following parameters.

  • strB - The string to compare with this instance.

Returns

String.CompareTo(String) method returns A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the value parameter. Value Condition Less than zero This instance precedes strB. Zero This instance has the same position in the sort order as strB. Greater than zero This instance follows strB. -or- strB is null.

Example

For example, a 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.


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

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

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

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