VB.NET Comparing Strings
In this article I explain how to compare strings in VB.NET.
The Compare method compares two strings and returns an integer value. The return value of Compare method can be less than zero, greater than zero or equals to zero.
Value
|
Meaning
|
Less than zero
|
When first string is less than second.
|
Zero
|
When both strings are equal.
|
Greater than zero
|
When first string is greater than zero.
|
The following code compares two strings and return results on the System console.
// Comparing two strings
//====================================
Module Module1
Sub Main()
Dim str1 As String
Dim str2 As String
str1 = "abcd"
str2 = "ABCD"
Dim result As Integer
result = String.Compare(str1, str2)
Console.WriteLine(result)
result = String.Compare(str1, str2, True)
Console.WriteLine(result)
Console.ReadLine()
End Sub
End Module
//====================================
The Compare method is an instance method. It compares a value (either a string or on object) with a string instance. Return values of this method are same as the Compare method. The following source code compares two strings.
// Compare Method
str1 = "abcd"
str2 = "ABCD"
Dim result As Integer
result = String.Compare(str1, str2)
Conclusion
Hope this article would have helped you in understanding Compare Method in VB.NET.