SoFunction
Updated on 2025-03-10

VBS tutorial: Function-StrComp Function

StrComp function

Returns a value indicating the result of the string comparison.

StrComp(string1, string2[, compare])

parameter

string1

Required option. Any valid string expression.

string2

Required option. Any valid string expression.

Compare

Optional. A numeric value indicating the comparison type used when calculating a string. If omitted, a binary comparison is performed. For values, see the Settings section.

set up

compareThe parameters can have the following values:

constant value describe
vbBinaryCompare 0 Perform binary comparisons.
vbTextCompare 1 Perform text comparison.

Return value

StrCompThe function has the following return value:

if StrComp Return
string1 is less than string2 -1
string1 equals string2 0
string1 is greater than string2 1
string1orstring2For Null Null

illustrate

The following example usesStrCompThe function returns the result of a string comparison. If the third parameter is 1, perform text comparison; if the third parameter is 0, perform binary comparison.

Dim MyStr1, MyStr2, MyCompMyStr1 = "ABCD": MyStr2 = "abcd"       'Define variables。MyComp = StrComp(MyStr1, MyStr2, 1)    ' return 0MyComp = StrComp(MyStr1, MyStr2, 0)    ' return -1MyComp = StrComp(MyStr2, MyStr1)       ' return 1