StrComp() String Comparison in Classic ASP

StrComp() compares two strings in Classic ASP and returns -1, 0 or 1. Use vbBinaryCompare to respect letter case, or vbTextCompare to compare text without distinguishing uppercase and lowercase letters.

Here is a complete example that compares the same values in both modes. Save the code in a .asp file and run it on IIS:

<%
Dim string1, string2
string1 = "Plus2net.com"
string2 = "plus2net.com"
Response.Write "Binary comparison: " & StrComp(string1, string2, vbBinaryCompare) & "<br>"
Response.Write "Text comparison: " & StrComp(string1, string2, vbTextCompare)
%>

Output:

Binary comparison: -1
Text comparison: 0

StrComp() syntax in VBScript

StrComp(string1, string2[, compare])

string1 and string2 are the values to compare. The optional third argument controls how they are compared. In VBScript, omitting it uses binary comparison.

Comparison modeValueBehavior
vbBinaryCompare0Compares string values with a binary, case-sensitive comparison.
vbTextCompare1Performs a text comparison that does not distinguish uppercase and lowercase letters.
Argument omittedDefaultIn VBScript, the comparison is binary. Explicitly naming the mode makes your intention easier to see.

The constants vbBinaryCompare and vbTextCompare are available in Classic ASP VBScript. Use the readable names rather than unexplained 0 and 1 arguments in your application code.

Understanding the StrComp() return values

ResultMeaningTypical use
-1The first string sorts before the second.Check a less-than ordering result.
0The two strings compare equal under the chosen mode.Check whether two strings match.
1The first string sorts after the second.Check a greater-than ordering result.
NullAt least one of the string arguments is Null.Detect missing nullable values using IsNull().

For example, these three comparisons illustrate ordering with lowercase English words:

<%
Response.Write StrComp("apple", "banana", vbBinaryCompare) & "<br>"
Response.Write StrComp("apple", "apple", vbBinaryCompare) & "<br>"
Response.Write StrComp("banana", "apple", vbBinaryCompare)
%>

Output:

-1
0
1

When you only need equality, test whether the result equals 0. When you need ordering, be aware that textual comparisons can depend on language and locale rules.

Case-sensitive comparison using vbBinaryCompare

The original Plus2Net example uses different capitalizations of the same website name. A binary comparison detects the difference.

<%
Dim string1, string2
string1="Plus2net.com"
string2="plus2net.com"
Response.Write StrComp(string1, string2, vbBinaryCompare)
%>

Output:

-1

The uppercase P in Plus2net.com sorts before the lowercase p in a binary comparison. The comparison does not change either string.

Case-insensitive comparison using vbTextCompare

Change the third argument to vbTextCompare to treat the two versions of the name as equal:

<%
Dim string1, string2
string1 = "Plus2net.com"
string2 = "plus2net.com"
Response.Write StrComp(string1, string2, vbTextCompare)
%>

Output:

0

Text comparison is useful for matching user-entered labels or categories where capitalization should not change the result. For password checks, tokens, and other security-sensitive identifiers, do not assume a case-insensitive text comparison is appropriate.

Comparison table: binary versus text

The following table retains the original examples and demonstrates how the selected mode affects the result.

string1string2ModeResult
Plus2net.complus2net.comvbBinaryCompare-1
Plus2net.complus2net.comvbTextCompare0
Plus2net.comPlus2net.comvbBinaryCompare0
plus2net.comPlus2net.comvbBinaryCompare1

Use StrComp() with an If...Then condition

A result of 0 can be used inside an If...Then...Else condition. Select the comparison mode based on the type of identifier being checked.

<%
Dim suppliedCode, expectedCode
suppliedCode = "ABC123"
expectedCode = "abc123"
If StrComp(suppliedCode, expectedCode, vbBinaryCompare) = 0 Then
    Response.Write "Exact match"
Else
    Response.Write "Different text or capitalization"
End If
%>

Output: Different text or capitalization. Here a binary comparison is used because the strings must match exactly.

Check equality without changing the original strings

You do not have to convert both values with UCase() or LCase() before checking for a case-insensitive match. StrComp(..., vbTextCompare) compares the inputs directly:

<%
Dim firstText, secondText
firstText = "ASP Tutorials"
secondText = "asp tutorials"
If StrComp(firstText, secondText, vbTextCompare) = 0 Then
    Response.Write "The names match (case-insensitive)"
Else
    Response.Write "The names differ"
End If
%>

Output: The names match (case-insensitive). Both original strings remain unchanged after the comparison.

Ignore surrounding spaces before comparing

StrComp() does not automatically discard spaces. An extra space at either end changes the string, so use Trim() first if your application treats surrounding spaces as accidental.

<%
Dim category1, category2
category1 = "  ASP Tutorials  "
category2 = "asp tutorials"
If StrComp(Trim(category1), Trim(category2), vbTextCompare) = 0 Then
    Response.Write "Categories match"
Else
    Response.Write "Categories differ"
End If
%>

Output: Categories match. This removes surrounding ordinary spaces but does not collapse spaces between words; see Replace() if the interior text needs a known transformation.

Comparing Null and empty strings

If either input to StrComp() is Null, the result is Null, rather than -1, 0 or 1. Check possible database or nullable values with IsNull() before comparing:

<%
Dim firstValue, secondValue, comparisonResult
firstValue = Null
secondValue = "Classic ASP"
If IsNull(firstValue) Or IsNull(secondValue) Then
    Response.Write "A value is Null; comparison skipped"
Else
    comparisonResult = StrComp(firstValue, secondValue, vbTextCompare)
    Response.Write comparisonResult
End If
%>

Output: A value is Null; comparison skipped.

Empty strings are different from Null

An empty string ("") is a valid string of length zero. You can compare empty strings directly:

<%
Response.Write StrComp("", "", vbBinaryCompare) & "<br>"
Response.Write StrComp("", "ASP", vbBinaryCompare) & "<br>"
Response.Write StrComp("ASP", "", vbBinaryCompare)
%>

Output:

0
-1
1

For form input, use Len(Trim(value)) when checking whether a visitor supplied any non-space text, and validate expected values on the server.

Compare a submitted HTML form value in Classic ASP

This self-contained example reads a POST field from the current ASP page and checks whether the visitor selected ASP. It compares without regard to letter case and HTML-encodes unexpected input before displaying it.

<%
Dim userCategory
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    userCategory = Trim(Request.Form("category"))
    If Len(userCategory) = 0 Then
        Response.Write "<p>Please enter a category.</p>"
    ElseIf StrComp(userCategory, "ASP", vbTextCompare) = 0 Then
        Response.Write "<p>You selected the ASP category.</p>"
    Else
        Response.Write "<p>You entered: " & Server.HTMLEncode(userCategory) & "</p>"
    End If
End If
%>
<form method="post" action="">
  <label for="category">Category</label>
  <input type="text" id="category" name="category" required>
  <button type="submit">Check category</button>
</form>

Submit asp, ASP, or Asp to get the same category match. A GET or POST form does not replace proper server-side validation or protection for state-changing actions. For more background, see ASP GET and POST form handling.

Find matching text inside an array

Use StrComp() in a loop to find every array item equal to a target string. This example finds both capitalizations of ASP:

<%
Dim subjects, subject
subjects = Array("ASP", "PHP", "asp", "Python")
For Each subject In subjects
    If StrComp(subject, "ASP", vbTextCompare) = 0 Then
        Response.Write Server.HTMLEncode(subject) & "<br>"
    End If
Next
%>

Output:

ASP
asp

For other array tasks, see VBScript Filter() and Split() for turning input text into an array.

Using StrComp() to compare alphabetical order

The return value can also tell you whether one text value comes before or after another under the chosen comparison mode. This is useful for explaining ordering, but sorting language-specific text may depend on locale.

<%
Dim firstName, secondName, result
firstName = "Blue"
secondName = "Green"
result = StrComp(firstName, secondName, vbTextCompare)
If result < 0 Then
    Response.Write "First value comes before second value"
ElseIf result > 0 Then
    Response.Write "First value comes after second value"
Else
    Response.Write "Both values are equal"
End If
%>

Output: First value comes before second value.

What if the compare argument is omitted?

The VBScript function uses binary comparison when the third argument is omitted. It is better to specify a mode explicitly whenever the distinction matters:

<%
Response.Write StrComp("Plus2net.com", "plus2net.com")
%>

Output: -1.

Common issues with StrComp()

IssueCauseSuggested approach
Two names differ only in capitalization but do not match.The comparison used vbBinaryCompare.Use vbTextCompare when case differences should be ignored.
Apparently identical strings compare differently.There may be leading spaces, trailing spaces, or other invisible characters.Inspect the values and use Trim() when appropriate; trim does not remove every whitespace character.
The result is Null.One of the compared string arguments is Null.Check nullable inputs with IsNull() before treating the result as numeric.
A comparison unexpectedly matches an identifier.Text comparison may ignore capitalization.Choose an exact-comparison strategy for identifiers and security-sensitive values.
Unexpected results for accented or multilingual text.Text comparisons can use language-sensitive ordering rules.Test the intended locale and compare behavior with the application's actual input language.

Frequently asked questions

What does StrComp() return when two strings are equal?

It returns 0 when the two values are considered equal under the chosen comparison mode.

Does StrComp() ignore uppercase and lowercase letters?

Only when you use vbTextCompare. Use vbBinaryCompare for a binary case-sensitive comparison.

Is StrComp() the same as comparing two strings with =?

Both can test string equality, but StrComp() also returns ordering information and lets you explicitly choose the comparison mode.

Does StrComp() change the original string?

No. It returns a comparison result, and neither input string is modified.

Can StrComp() compare empty strings?

Yes. Two empty strings compare equal and return 0. Handle Null separately because it is not the same as an empty string.

Should I use StrComp() or InStr()?

Use StrComp() to compare whole string values. Use InStr() when you need to find a substring inside a larger text value.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer