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

Description

String Compare(String, Int32, String, Int32, Int32) compares substrings of two specified String objects and returns an integer that indicates their relative position in the sort order.

Syntax

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


public static int Compare(
  string strA,/*  w  w w  . jav a2 s. co  m*/
  int indexA,
  string strB,
  int indexB,
  int length
)

Parameters

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

  • strA - The first string to use in the comparison.
  • indexA - The position of the substring within strA.
  • strB - The second string to use in the comparison.
  • indexB - The position of the substring within strB.
  • length - The maximum number of characters in the substrings to compare.

Returns

String.Compare(String, Int32, String, Int32, Int32) method returns A 32-bit signed integer indicating the lexical relationship between the two comparands. 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 compares two substrings. Sample for String.Compare(String, Int32, String, Int32, Int32)


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

class Sample {
    public static void Main() {

        String str1 = "java2s.com";
        String str2 = "asdfasdfasdf";
        String str;
        int result;
    
        result = String.Compare(str1, 2, str2, 0, 2);
        str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
        Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
        Console.Write("{0} ", str);
        Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
    }
}

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