Compare Method (String, Int32, Int32, String, Int32, Int32)

CompareInfo.Compare Method (String, Int32, Int32, String, Int32, Int32)

 

Compares a section of one string with a section of another string.

Namespace:   System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Public Overridable Function Compare (
	string1 As String,
	offset1 As Integer,
	length1 As Integer,
	string2 As String,
	offset2 As Integer,
	length2 As Integer
) As Integer

Parameters

string1
Type: System.String

The first string to compare.

offset1
Type: System.Int32

The zero-based index of the character in string1 at which to start comparing.

length1
Type: System.Int32

The number of consecutive characters in string1 to compare.

string2
Type: System.String

The second string to compare.

offset2
Type: System.Int32

The zero-based index of the character in string2 at which to start comparing.

length2
Type: System.Int32

The number of consecutive characters in string2 to compare.

Return Value

Type: System.Int32

A 32-bit signed integer indicating the lexical relationship between the two comparands.

Value

Condition

zero

The two strings are equal.

less than zero

The specified section of string1 is less than the specified section of string2.

greater than zero

The specified section of string1 is greater than the specified section of string2.

Exception Condition
ArgumentOutOfRangeException

offset1 or length1 or offset2 or length2 is less than zero.

-or-

offset1 is greater than or equal to the number of characters in string1.

-or-

offset2 is greater than or equal to the number of characters in string2.

-or-

length1 is greater than the number of characters from offset1 to the end of string1.

-or-

length2 is greater than the number of characters from offset2 to the end of string2.

If a security decision depends on a string comparison or a case change, you should use the InvariantCulture property to ensure that the behavior is consistent regardless of the culture settings of the operating system.

System_CAPS_noteNote

When possible, you should use string comparison methods that have a parameter of type CompareOptions to specify the kind of comparison expected. As a general rule, use linguistic options (using the current culture) for comparing strings displayed in the user interface and specify Ordinal or OrdinalIgnoreCase for security comparisons.

Notes to Callers:

Character sets include ignorable characters. The Compare(String, Int32, Int32, String, Int32, Int32) method does not consider these characters when it performs a linguistic or culture-sensitive comparison. To recognize ignorable characters in your comparison, call the Compare(String, Int32, Int32, String, Int32, Int32, CompareOptions) method and supply a value of CompareOptions.Ordinal or CompareOptions.OrdinalIgnoreCase for the options parameter.

The following example compares portions of two strings using the different CompareInfo objects:

Imports System
Imports System.Globalization

Public Class SamplesCompareInfo

   Public Shared Sub Main()

      ' Defines the strings to compare.
      Dim myStr1 As [String] = "calle"
      Dim myStr2 As [String] = "calor"

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with international sort.
      Dim myCompIntl As CompareInfo = CompareInfo.GetCompareInfo("es-ES")

      ' Uses GetCompareInfo to create the CompareInfo that uses the "es-ES" culture with traditional sort.
      Dim myCompTrad As CompareInfo = CompareInfo.GetCompareInfo(&H40A)

      ' Uses the CompareInfo property of the InvariantCulture.
      Dim myCompInva As CompareInfo = CultureInfo.InvariantCulture.CompareInfo

      ' Compares two strings using myCompIntl.
      Console.WriteLine("Comparing ""{0}"" and ""{1}""", myStr1.Substring(2, 2), myStr2.Substring(2, 2))
      Console.WriteLine("   With myCompIntl.Compare: {0}", myCompIntl.Compare(myStr1, 2, 2, myStr2, 2, 2))
      Console.WriteLine("   With myCompTrad.Compare: {0}", myCompTrad.Compare(myStr1, 2, 2, myStr2, 2, 2))
      Console.WriteLine("   With myCompInva.Compare: {0}", myCompInva.Compare(myStr1, 2, 2, myStr2, 2, 2))

   End Sub 'Main 

End Class 'SamplesCompareInfo


'This code produces the following output.
'
'Comparing "ll" and "lo"
'   With myCompIntl.Compare: -1
'   With myCompTrad.Compare: 1
'   With myCompInva.Compare: -1

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft