O'Reilly logo

VBScript in a Nutshell, 2nd Edition by Ron Petrusha, Matt Childs, Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

StrComp Function

Syntax

StrComp(string1, string2[, compare])
string1

Use: Required

Data Type: String

Any string expression.

string2

Use: Required

Data Type: String

Any string expression.

compare

Use: Optional

Data Type: Integer constant

The type of string comparison to perform.

Return Value

An Integer.

Description

Determines whether two strings are equal and which of two strings is greater.

Rules at a Glance

  • The following intrinsic constants are available as the settings for compare:

Constant

Value

Comparison to perform

vbBinaryCompare

0

Binary (default)

vbTextCompare

1

Textual

  • If compare isn’t specified, its value defaults to vbBinaryCompare. In other words, the comparison of string1 and string2 is case-sensitive.

  • This table describes the possible return values from the StrComp function:

Scenario

Return value

string1 < string2

-1

string1 = string2

0

string1 > string2

1

string1 or string2 is Null

Null

Programming Tips and Gotchas

  • If you just need to know whether string1 is greater than string2 (or vice versa), couldn’t you simply use the < or > comparison operators? When you’re dealing with strings of characters, VBScript sees each character as a number. Simply using the comparison operators therefore compares the numerical value of one string with the other. Take this scenario:

    Dim sString1
    Dim sString2
    
    sString1 = "hello world"
    sString2 = "HELLO WORLD"

    Subjectively, because of the significance of uppercase letters in text, we’d probably say that sString2 is greater than sString1. But VBScript sees ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required