StrComp - VBScript
Overview
The StrComp
command in VB Script compares two strings and returns a numeric value indicating their relative order. It is commonly used to sort or compare text values in scripts.
Syntax
StrComp(string1, string2, [compareOption])
Parameters:
- string1 (required): The first string to compare.
- string2 (required): The second string to compare.
- compareOption (optional): Specifies the comparison criteria.
Options/Flags
- CompareOption:=0 (Default): Binary comparison based on string values.
- CompareOption:=1: Text comparison, case-insensitive.
- CompareOption:=2: Text comparison, case-sensitive.
Examples
Simple Comparison:
WScript.Echo StrComp("Apple", "Orange") 'Output: -1 (Apple comes before Orange alphabetically)
Case-Insensitive Comparison:
WScript.Echo StrComp("Apple", "APPLE", 1) 'Output: 0 (Same string, case-insensitive)
Complex Comparison with Leading Spaces:
WScript.Echo StrComp(" Apple", "Apple", 2) 'Output: 1 (Leading spaces make strings unequal, case-sensitive)
Common Issues
- Incorrect comparison values: Ensure that the correct string values and comparison options are used.
- Null or empty strings: Handle null or empty strings explicitly to avoid errors.
Integration
StrComp
can be combined with other VB Script commands for advanced operations:
Sub SortArray()
Dim arr = Array("Apple", "Orange", "Banana")
arr.Sort StrComp
For i = 0 To arr.UBound
WScript.Echo arr(i)
Next
End Sub
Related Commands
InStr
: Search for a substring in a string.LCase
: Convert a string to lowercase.UCase
: Convert a string to uppercase.Trim
: Remove leading and trailing spaces from a string.