String Operations
This chapter describes:
- Concatenation
- String Comparison Operations
Notes and samples in this chapter are based Visual Basic 6.0.
Concatenation
Concatenation is a string operation that:
- Operates on two string operands.
- Produces a new string by concatenating the second operand to the end of the first operand.
- Uses (&) as the operator.
See the next section for example on how concatenation works.
String Comparison Operations
String comparison operations are operations that:
- Operates on two string operands.
- Produces a Boolean value by applying the comparison operation specified by the operator.
There are 6 string comparison operations supported in VB:
- 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_operation.html:
<html>
<body>
<!-- string_operation.html
Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
document.writeln("Hello " & "world!")
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:
Hello world!
False
True
True
False
False
True
No surprises in the output.
Conclusions
- Use (&) operator to concatenate strings.
- String comparisons are based character ASCII values.
|