VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

String Comparison Operation

This section provides a quick introduction of string comparison operations, which compare one character at a time based its ASCII value.

String comparison operations are operations that:

  • Operates on two operands of String values.
  • Produces a Boolean value by applying the string comparison operation specified by the operator.
  • String comparison operations are performed one character at a time with its ASCII value.
  • String comparison operations use same operators as numeric comparison operations: =, <>, <, >, <=, >=.

There are 6 string comparison operations supported in VBScript:

  • Equal to (=): Resulting (True) if two strings have the same length and the same characters at the same positions.
  • Not equal to (<>): Resulting (True) if two strings are not equal.
  • Greater than (>): Resulting (True) if the first string is greater than the second string. The logic of (>) operation works by comparing every character in the first string with the character at the same position in the second string from left to right. If there is no character left in the second string, stop operation and return (True); Or if the character in the first string has a higher ASCII value, stop operation and return (True); Or if two characters are identical, continue to the next character; Otherwise, stop operation and return (False).
  • Less than (<): Resulting (True) if two strings are not equal, and the first string is not greater than the second string.
  • Greater than or equal to (>=): Resulting (True) if the first string is greater than the second string or two strings are equal.
  • Less than or equal to (<=): Resulting (True) if the first string is less than the second string or two strings are equal.

To show you how string operations work, I wrote the following script, string_comparison.html:

<html>
<body>
<!-- string_comparison.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   document.writeln("ABC" = "abc")
   document.writeln("ABC" <> "abc")
   document.writeln("abc " > "abc")
   document.writeln("abc" < "abc")
   document.writeln("abc" >= "abd")
   document.writeln("abc" <= "abc")
</script>
</pre>
</body>
</html>

Here is the output of this VBScript example script:

False
True
True
False
False
True

The output confirms some of string comparison operation rules:

  • ("ABC" = "abc") returns False, because the first character of the first operand is not equal to the first character of the second operand.
  • ("abc " > "abc") returns True, because the first operand has one more character than the second operand, and all other 3 characters are identical in both operands.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 Numeric Comparison Operations and Logical Operations

String Operations - Concatenation and Comparison

 String Concatenation Operation

String Comparison Operation

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

String Comparison Operation - Updated in 2015, by Dr. Herong Yang