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(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 mode | Value | Behavior |
|---|---|---|
vbBinaryCompare | 0 | Compares string values with a binary, case-sensitive comparison. |
vbTextCompare | 1 | Performs a text comparison that does not distinguish uppercase and lowercase letters. |
| Argument omitted | Default | In 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.
| Result | Meaning | Typical use |
|---|---|---|
-1 | The first string sorts before the second. | Check a less-than ordering result. |
0 | The two strings compare equal under the chosen mode. | Check whether two strings match. |
1 | The first string sorts after the second. | Check a greater-than ordering result. |
Null | At 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.
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.
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.
The following table retains the original examples and demonstrates how the selected mode affects the result.
| string1 | string2 | Mode | Result |
|---|---|---|---|
| Plus2net.com | plus2net.com | vbBinaryCompare | -1 |
| Plus2net.com | plus2net.com | vbTextCompare | 0 |
| Plus2net.com | Plus2net.com | vbBinaryCompare | 0 |
| plus2net.com | Plus2net.com | vbBinaryCompare | 1 |
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.
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.
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.
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.
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.
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.
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.
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.
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.
| Issue | Cause | Suggested 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. |
It returns 0 when the two values are considered equal under the chosen comparison mode.
Only when you use vbTextCompare. Use vbBinaryCompare for a binary case-sensitive comparison.
Both can test string equality, but StrComp() also returns ordering information and lets you explicitly choose the comparison mode.
No. It returns a comparison result, and neither input string is modified.
Yes. Two empty strings compare equal and return 0. Handle Null separately because it is not the same as an empty string.
Use StrComp() to compare whole string values. Use InStr() when you need to find a substring inside a larger text value.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.