VB.Net program to compare two strings

Here, we are going to learn how to compare two strings in VB.Net?
Submitted by Nidhi, on December 03, 2020 [Last updated : March 07, 2023]

Comparing two strings in VB.Net

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

Program/Source Code:

The source code to compare two strings is given below. The given program is compiled and executed successfully.

VB.Net code to compare two strings

'VB.Net program to compare two strings.

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
Press any key to continue . . .

Explanation

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

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

In the above code we use if statements to compare two strings, and then we printed appropriate message on the console screen.

VB.Net String Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.