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

Description

String Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) compares substrings of 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 substrings to each other in the sort order.

Syntax

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


public static int Compare(
  string strA,/* w w w  . j av a 2 s .  com*/
  int indexA,
  string strB,
  int indexB,
  int length,
  CultureInfo culture,
  CompareOptions options
)

Parameters

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

  • strA - The first string to use in the comparison.
  • indexA - The starting position of the substring within strA.
  • strB - The second string to use in the comparison.
  • indexB - The starting position of the substring within strB.
  • length - The maximum number of characters in the substrings to compare.
  • culture - An object that supplies culture-specific comparison information.
  • options - Options to use when performing the comparison (such as ignoring case or symbols).

Returns

String.Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method returns An integer that indicates the lexical relationship between the two substrings, as shown in the following table. Value Condition Less than zero The substring in strA is less than the substring in strB. Zero The substrings are equal or length is zero. Greater than zero The substring in strA is greater than the substring in strB.

Example

The following example uses the Compare(String, Int32, String, Int32, Int32, CultureInfo, CompareOptions) method to compare the last names of two people. It then lists them in alphabetical order.


using System;//from www.j  ava2  s. c o  m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string name1 = "Java c# Javascript";
      string name2 = "Java c# Javascript";

      // Get position of space character. 
      int index1 = name1.IndexOf(" ");
      index1 = index1 < 0 ? 0 : index1--;

      int index2 = name2.IndexOf(" ");
      index1 = index1 < 0 ? 0 : index1--;

      int length = Math.Max(name1.Length, name2.Length);

      Console.WriteLine("Sorted alphabetically by last name:");
      if (String.Compare(name1, index1, name2, index2, length, 
                         new CultureInfo("en-US"), CompareOptions.IgnoreCase) < 0)
         Console.WriteLine("{0}\n{1}", name1, name2); 
      else
         Console.WriteLine("{0}\n{1}", name2, name1); 
   }
}

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