StrComp Function in VB.NET

How to use the StrComp function in VB.NET?

Explanation

StrComp Function

StrComp String Function in Visual Basic.net 2008 is used to compare two strings to return values '-1', '1', '0' based on the value.
Syntax:

Function StrComp(ByVal String1 As String1,
ByVal String2 As String,
Optional Comparison Method) As Integer

In the above syntax, String1 and String2 specifies the two strings to compare. The optional comparison method can have Binary or Text for text and binary comparison.
Example:

Module Module1
Sub Main()
Dim Str1 As String = "Hscripts"
Dim Str2 As String = "hscripts"
Console.WriteLine("Result of string comparison is:: "
& StrComp(Str1, Str2, CompareMethod.Binary))
Console.ReadLine()
End Sub
End Module
Result:

Result of string comparison is:: -1

In the above example, since Str1 and Str2 are compared using a binary method. Both the strings are similar except that the first character is in uppercase in str1, so it sorts ahead to return '-1'. Thus StrComp Function can be used.

Visual Basic Tutorial


Ask Questions

Ask Question