ADVERTISEMENT
ADVERTISEMENT

VB.Net program to demonstrate the Compare() method of String class

Here, we are going to demonstrate the Compare() method of String class in VB.Net.
Submitted by Nidhi, on January 20, 2021

The Compare() method is used to compare two strings.

Syntax:

Function Compare(ByVal str1 as String, ByVal str2 as String) as integer

Parameters:

  • Str1: String to be compared.
  • Str2: String to be compared.

Return Value: It returns an integer value, if both strings are matched then it will return 0 otherwise it will return a non-zero value.

Program/Source Code:

The source code to demonstrate the Compare() method of the String class is given below. The given program is compiled and executed successfully.

'VB.NET program to demonstrate the Compare() 
'method of String class.

Imports System

Module Module1
    Sub Main()
        Dim str1 As String = "Hello"
        Dim str2 As String = "hello"
        Dim str3 As String = "Hello"

        Dim ret As Integer = 0

        ret = String.Compare(str1, str2)
        If ret = 0 Then
            Console.WriteLine("Strings matched")
        Else
            Console.WriteLine("Strings are different")
        End If

        ret = String.Compare(str1, str3)
        If ret = 0 Then
            Console.WriteLine("Strings matched")
        Else
            Console.WriteLine("Strings are different")
        End If

        Console.WriteLine()
    End Sub
End Module

Output:

Strings are different
Strings matched

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 Compare() method of the String class. This method returns 0 when the values of specified strings are matched otherwise it will return a non-zero value. Here, we printed the appropriate message according to string values on the console screen.

VB.Net String Programs »



ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.