How to VB.NET String.Compare()

The VB.NET String Compare function is a powerful tool that allows you to compare two String objects in a variety of ways, enabling you to determine the relationship between them based on factors such as case sensitivity, culture-specific rules, and comparison options. This function provides flexibility and control in comparing strings, ensuring accurate and reliable results for various string comparison scenarios in your VB.NET applications.

System.String.Compare(String str1,String str2, Boolean ) As Integer

String Compare function returns an Integer value that indicates the lexical relationship between the two strings being compared. This comparison considers factors such as the characters' relative order, case sensitivity, and cultural-specific rules. The returned value provides valuable information about whether one string is less than, equal to, or greater than the other, aiding in sorting, searching, and other string manipulation operations within VB.NET applications.

  1. String str1 : Parameter String.
  2. String str2 : Parameter String.

The Boolean value returned by the VB.NET String Compare function provides an indication of whether the comparison between two strings is case-sensitive or case-insensitive. When the returned value is True, it indicates that the string comparison was performed with case sensitivity, meaning that uppercase and lowercase letters are considered distinct.

On the other hand, when the returned value is False, it signifies that the string comparison was conducted in a case-insensitive manner, where uppercase and lowercase letters are treated as equal. This feature allows you to control and adapt the string comparison behavior based on your specific requirements and sensitivity to letter case.

  1. Integer : returns less than zero, zero or greater than zero.
  2. Less than zero : str1 is less than str2.
  3. Zero : str1 is equal to str2.
  4. Greater than zero : str1 is grater than str2.
Full Source VB.NET
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim str1 As String Dim str2 As String str1 = "" str2 = "VB.NET" Dim result As Integer result = String.Compare(str1, str2) MsgBox(result) result = String.Compare(str1, str2, True) MsgBox(result) End Sub End Class


The VB.NET String.Compare() function allows you to compare two strings and obtain information about their lexical relationship, including case sensitivity, culture-specific rules, and comparison options.