Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the Equals() method of String class
By Nidhi Last Updated : November 11, 2024
String.Equals() Method in VB.Net
The Equals() method is used to check the equality of two strings and return a Boolean value.
Syntax
Function Equals(ByVal str1 as String, ByVal str2 as String) as Boolean
Parameter(s)
- Str1: String to be compared.
- Str2: String to be compared.
Return Value
It returns a boolean value, if both strings are equal then it will return true otherwise it will return false.
VB.Net code to demonstrate the example of String.Equals() method
The source code to demonstrate the Equals() method of the String class is given below. The given program is compiled and executed successfully.
'VB.NET program to demonstrate the Equals() method
'of String class.
Imports System
Module Module1
Sub Main()
Dim str1 As String = "hello World"
Dim str2 As String = "Hello World"
Dim str3 As String = "Hello World"
Dim ret As Boolean
ret = String.Equals(str1, str2)
If ret = True Then
Console.WriteLine("Strings str1 and str2 are equal")
Else
Console.WriteLine("Strings str1 and str2 are not equal")
End If
ret = String.Equals(str2, str3)
If ret = True Then
Console.WriteLine("Strings str2 and str3 are equal")
Else
Console.WriteLine("Strings str2 and str3 are not equal")
End If
End Sub
End Module
Output
Strings str1 and str2 are not equal
Strings str2 and str3 are equal
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.
In the Main() function, we created three strings str1, str2, and str3. Here, we compared the values of strings using the Equals() method of the String class. This method returns true when the values of specified strings are equal otherwise it will return a false value. Here, we printed the appropriate message according to string values on the console screen.
VB.Net String Programs »