How to VB.NET String.Equals()

The VB.NET String.Equals() function serves the purpose of comparing the values of two String objects to determine if they are identical or not. It allows you to perform a case-sensitive or case-insensitive comparison, depending on the specified parameters. By using the String.Equals() function, you can validate whether two strings have the same content, which is particularly useful when performing string comparisons and implementing conditional logic in VB.NET applications.

String.Equals() method

Here is an example of how to use the String.Equals() method in VB.NET:

Dim str1 As String = "Hello" Dim str2 As String = "HELLO" Dim result1 As Boolean = String.Equals(str1, str2) ' Case-sensitive comparison Console.WriteLine(result1) ' Output: False Dim result2 As Boolean = String.Equals(str1, str2, StringComparison.OrdinalIgnoreCase) ' Case-insensitive comparison Console.WriteLine(result2) ' Output: True

In the above example, we have two string variables str1 and str2 with different case variations.

In the first comparison, we use the default behavior of String.Equals() which performs a case-sensitive comparison. The result is False because "Hello" is not the same as "HELLO" when considering the case.

In the second comparison, we use the StringComparison.OrdinalIgnoreCase parameter to perform a case-insensitive comparison. This time, the result is True because "Hello" and "HELLO" are considered equal regardless of the case.

Conclusion

By using the String.Equals() method, you can easily compare string values and determine their equality based on your desired comparison rules.