C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Tip: Compare returns just three values: -1, 0 and 1. This number indicates the relation of the two strings being compared.
Next: The result of 1 indicates that String "b" is larger than String a. And 0 means the two Strings are equal.
CompareOrdinal: This performs the same logic but treats each character as an ordinal value. This means Chars are treated by their numeric value.
CompareTo: With CompareTo, we use String instances to perform the comparison. The results of CompareTo are the same as the results of String.Compare.
VB.NET program that uses compare functions
Module Module1
Sub Main()
Dim a As String = "a"
Dim b As String = "b"
Dim c As Integer = String.Compare(a, b)
Console.WriteLine(c)
c = String.CompareOrdinal(b, a)
Console.WriteLine(c)
c = a.CompareTo(b)
Console.WriteLine(c)
c = b.CompareTo(a)
Console.WriteLine(c)
c = "x".CompareTo("x")
Console.WriteLine(c)
End Sub
End Module
Output
-1
1
-1
1
0
And: Internally, those methods will use a Compare method based on the Enum argument.
EnumSo: StringComparison.Ordinal will result in the CompareOrdinal method being used in some way.
LastIndexOf