This documentation is archived and is not being maintained.

Strings.StrComp Method

Returns -1, 0, or 1, based on the result of a string comparison.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static int StrComp(
	string String1,
	string String2,
	CompareMethod Compare


Type: System.String
Required. Any valid String expression.
Type: System.String
Required. Any valid String expression.
Type: Microsoft.VisualBasic.CompareMethod
Optional. Specifies the type of string comparison. If Compare is omitted, the Option Compare setting determines the type of comparison.

Return Value

Type: System.Int32
The StrComp function has the following return values.


StrComp returns

String1 sorts ahead of String2


String1 is equal to String2


String1 sorts after String2



Compare value is not valid.

The strings are compared by alphanumeric sort values beginning with the first character. For further information on binary comparisons, textual comparisons, and sort order, see Option Compare Statement.

The Compare argument settings are:




Performs a binary comparison, based on a sort order derived from the internal binary representations of the characters.


Performs a text comparison, based on a case-insensitive text sort order determined by your system's LocaleID value.

This example uses the StrComp function to return the results of a string comparison.

' Defines variables.
Dim TestStr1 As String = "ABCD"
Dim TestStr2 As String = "abcd"
Dim TestComp As Integer
' The two strings sort equally. Returns 0.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Text)
' TestStr1 sorts before TestStr2. Returns -1.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Binary)
' TestStr2 sorts after TestStr1. Returns 1.
TestComp = StrComp(TestStr2, TestStr1, CompareMethod.Binary)

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.