String comparison
Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:
Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses
The task is to demonstrate how to compare two strings from within the language and how to achieve a lexical comparison. The task should demonstrate:
- Comparing two strings for exact equality
- Comparing two strings for inequality (i.e., the inverse of exact equality)
- Comparing two strings to see if one is lexically lower than the other
- Comparing two strings to see if one is lexically higher than the other
Bonus:
- Demonstrate the other kinds of string comparisons that the language provides.
See also:
AWK
<lang awk>BEGIN {
a="BALL" b="BELL"
IF (a == b) { print "The strings are equal" } IF (a != b) { print "The strings are not equal" } IF (a > b) { print "The first string is lexically higher than the second" } IF (a < b) { print "The first string is lexically lower than the second" } IF (a >= b) { print "The first string is not lexically lower than the second" } IF (a <= b) { print "The first string is not lexically higher than the second" }
}</lang>
BASIC
<lang basic>10 LET "A$="BELL" 20 LET B$="BELT" 30 IF A$ = B$ THEN PRINT "THE STRINGS ARE EQUAL": REM TEST FOR EQUALITY 40 IF A$ <> B$ THEN PRINT "THE STRINGS ARE NOT EQUAL": REM TEST FOR INEQUALITY 50 IF A$ > B$ THEN PRINT A$;" IS LEXICALLY HIGHER THAN ";B$: REM TEST FOR LEXICALLY HIGHER 60 IF A$ < B$ THEN PRINT A$;" IS LEXICALLY LOWER THAN ";B$: REM TEST FOR LEXICALLY LOWER 70 IF A$ <= B$ THEN PRINT A$;" IS NOT LEXICALLY HIGHER THAN ";B$ 80 IF A$ >= B$ THEN PRINT A$;" IS NOT LEXICALLY LOWER THAN ";B$ 90 END</lang>
UNIX Shell
<lang sh>#!/bin/sh
A="Bell" B="Ball"
- Traditional test command implementations test for equality and inequality
- but do not have a lexical comparison facility
if [ $A = $B ] ; then
ECHO 'The strings are equal"
fi if [ $A != $B ] ; then
ECHO 'The strings are not equal"
fi</lang>