String.Compare Method (String, String, StringComparison)
Compares two specified String objects using the specified rules, and returns an integer that indicates their relative position in the sort order.
Assembly: mscorlib (in mscorlib.dll)
Public Shared Function Compare ( strA As String, strB As String, comparisonType As StringComparison ) As Integer
Parameters
- strA
-
Type:
System.String
The first string to compare.
- strB
-
Type:
System.String
The second string to compare.
- comparisonType
-
Type:
System.StringComparison
One of the enumeration values that specifies the rules to use in the comparison.
Return Value
Type: System.Int32A 32-bit signed integer that indicates the lexical relationship between the two comparands.
Value | Condition |
|---|---|
Less than zero | strA precedes strB in the sort order. |
Zero | strA is in the same position as strB in the sort order. |
Greater than zero | strA follows strB in the sort order. |
| Exception | Condition |
|---|---|
| ArgumentException | comparisonType is not a StringComparison value. |
| NotSupportedException | StringComparison is not supported. |
The comparisonType parameter indicates whether the comparison should use the current or invariant culture, honor or ignore the case of the comparands, or use word (culture-sensitive) or ordinal (culture-insensitive) sort rules.
One or both comparands can be null. By definition, any string, including the empty string (""), compares greater than a null reference; and two null references compare equal to each other.
The comparison terminates when an inequality is discovered or both strings have been compared. However, if the two strings compare equal to the end of one string, and the other string has characters remaining, the string with remaining characters is considered greater. The return value is the result of the last comparison performed.
Unexpected results can occur when comparisons are affected by culture-specific casing rules. For example, in Turkish, the following example yields the wrong results because the file system in Turkish does not use linguistic casing rules for the letter "i" in "file".
Shared Function IsFileURI(ByVal path As String) As Boolean If String.Compare(path, 0, "file:", 0, 5, True) = 0 Then Return True Else Return False End If End Function
Compare the path name to "file" using an ordinal comparison. The correct code to do this is as follows:
Notes to Callers:
Character sets include ignorable characters. The Compare(String, String, StringComparison) method does not consider such characters when it performs a culture-sensitive comparison. To recognize ignorable characters in your comparison, supply a value of StringComparison.Ordinal or OrdinalIgnoreCase for the comparisonType parameter.
The following example compares three versions of the letter "I". The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.
' This example demonstrates the ' System.String.Compare(String, String, StringComparison) method. Imports System Imports System.Threading Class Sample Public Shared Sub Main() Dim intro As String = "Compare three versions of the letter I using different " & _ "values of StringComparison." ' Define an array of strings where each element contains a version of the ' letter I. (An array of strings is used so you can easily modify this ' code example to test additional or different combinations of strings.) Dim threeIs(2) As String ' LATIN SMALL LETTER I (U+0069) threeIs(0) = "i" ' LATIN SMALL LETTER DOTLESS I (U+0131) threeIs(1) = "ı" ' LATIN CAPITAL LETTER I (U+0049) threeIs(2) = "I" Dim unicodeNames As String() = { _ "LATIN SMALL LETTER I (U+0069)", _ "LATIN SMALL LETTER DOTLESS I (U+0131)", _ "LATIN CAPITAL LETTER I (U+0049)" } Dim scValues As StringComparison() = { _ StringComparison.CurrentCulture, _ StringComparison.CurrentCultureIgnoreCase, _ StringComparison.InvariantCulture, _ StringComparison.InvariantCultureIgnoreCase, _ StringComparison.Ordinal, _ StringComparison.OrdinalIgnoreCase } ' Console.Clear() Console.WriteLine(intro) ' Display the current culture because the culture-specific comparisons ' can produce different results with different cultures. Console.WriteLine("The current culture is {0}." & vbCrLf, _ Thread.CurrentThread.CurrentCulture.Name) ' Determine the relative sort order of three versions of the letter I. Dim sc As StringComparison For Each sc In scValues Console.WriteLine("StringComparison.{0}:", sc) ' LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131) Test(0, 1, sc, threeIs, unicodeNames) ' LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049) Test(0, 2, sc, threeIs, unicodeNames) ' LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049) Test(1, 2, sc, threeIs, unicodeNames) Console.WriteLine() Next sc End Sub 'Main Protected Shared Sub Test(ByVal x As Integer, ByVal y As Integer, _ ByVal comparison As StringComparison, _ ByVal testI() As String, ByVal testNames() As String) Dim resultFmt As String = "{0} is {1} {2}" Dim result As String = "equal to" Dim cmpValue As Integer = 0 ' cmpValue = String.Compare(testI(x), testI(y), comparison) If cmpValue < 0 Then result = "less than" ElseIf cmpValue > 0 Then result = "greater than" End If Console.WriteLine(resultFmt, testNames(x), result, testNames(y)) End Sub 'Test End Class 'Sample ' 'This code example produces the following results: ' 'Compare three versions of the letter I using different values of StringComparison. 'The current culture is en-US. ' 'StringComparison.CurrentCulture: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.CurrentCultureIgnoreCase: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.InvariantCulture: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.InvariantCultureIgnoreCase: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.Ordinal: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) ' 'StringComparison.OrdinalIgnoreCase: 'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131) 'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049) 'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049) '
Available since 8
.NET Framework
Available since 2.0
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