Home >>VB.Net Programs >VB.Net program to compare two strings

VB.Net program to compare two strings

Write a VB.Net program to compare two strings

Here, we will read two strings and compare and print the appropriate message on the console screen using the equal to = operator.

Program :

Here is the source code for comparing the two strings. The program given is compiled and successfully executed.

Example


Module Module1
    Sub Main()
        Dim str1 As String
        Dim str2 As String
        Console.Write("Enter string1: ")
        str1 = Console.ReadLine()
        Console.Write("Enter string2: ")
        str2 = Console.ReadLine()
        If str1 = str2 Then
            Console.WriteLine("Strings are equal")
        Else
            Console.WriteLine("Strings are not equal")
        End If
    End Sub
End Module
	
Output:
Enter string1 : Abc
Enter string2 : abc
Strings are not equal

Explanation:

We created a Module1 module in the above program that contains a method Main(). We have created two string variables, str1 and str2, in the Main() method.

Console.Write("Enter string1: ")
str1 = Console.ReadLine()

Console.Write("Enter string2: ")
str2 = Console.ReadLine()

In the above code, we read two strings str1 and str2

If str1 = str2 Then
    Console.WriteLine("Strings are equal")
Else
    Console.WriteLine("Strings are not equal")
End If

We use statements in the above code to compare two strings, and then we print a suitable message on the console screen.


No Sidebar ads