String comparison: Difference between revisions
m (→{{header|AWK}}) |
m (wiki syntax) |
||
Line 1: | Line 1: | ||
{{draft task|Basic Data Operations}}{{basic data operation}} |
{{draft task|Basic Data Operations}}{{basic data operation}}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: |
|||
* Comparison of strings for equality |
* Comparison of strings for equality |
Revision as of 20:42, 22 February 2013
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:
- Comparison of strings for equality
- Comparison of strings for inequality
- Comparison of strings for one being lexically lower than the other
- Comparison of strings for one being lexically higher than the other
- Any other string comparative operators and features that the language provides.
See also:Integer comparison See also:Character matching
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>