Strings.StrComp Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Shared Function StrComp ( _
    String1 As String, _
    String2 As String, _
    Compare As CompareMethod _
) As Integer
public static int StrComp(
    string String1,
    string String2,
    CompareMethod Compare
)

Parameters

  • String1
    Type: System.String
    Required. Any valid String expression.
  • String2
    Type: System.String
    Required. Any valid String expression.
  • Compare
    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
If String1 sorts ahead of String2, StrComp returns -1. If String1 is equal to String2, StrComp returns 0. If String1 sorts after String2, StrComp returns 1.

Remarks

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 the Option Compare Statement in the Visual Basic documentation. 

The Compare argument settings are:

Constant

Description

Binary

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

Text

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

Examples

This example uses the StrComp function to return the results of a string comparison. If the third argument is omitted, the comparison type defined in the Option Compare statement or project defaults is performed.

' 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 after TestStr2. Returns -1.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Binary)
' TestStr2 sorts before TestStr1. Returns 1.
TestComp = StrComp(TestStr2, TestStr1)

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.