String comparison: Difference between revisions

From Rosetta Code
Content added Content deleted
m ({{header|Rexx}})
Line 51: Line 51:
if animal <= 'cat' then
if animal <= 'cat' then
say animal "is not lexically higher than cat"
say animal "is not lexically higher than cat"
/* The above comparative operators remove whitespace
/* The above comparative operators do not consider
leading and trailing whitespace when making comparisons. */
before making comparisons. To not strip whitespace
we need to use strict comparative operators */
if ' cat ' = 'cat' then
if ' cat ' = 'cat' then
say "this will print because whitespace is stripped"
say "this will print because whitespace is stripped"

/* To consider all whitespace in a comparison
we need to use strict comparative operators */


if ' cat ' == 'cat' then
if ' cat ' == 'cat' then
say "this will noit print because comparison is strict"</lang>
say "this will not print because comparison is strict"</lang>


Here are the strict comparative operators:
Here is a list of the strict comparative operators and their meaning:


* == Strictly Equal To
* == Strictly Equal To
Line 70: Line 72:
* !\<< Strictly Not Less Than
* !\<< Strictly Not Less Than
* !\>> Strictly Not Greater Than
* !\>> Strictly Not Greater Than









=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 22:51, 22 February 2013

String comparison is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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>

Rexx

<lang rexx>animal = 'dog' if animal = 'cat' then

 say animal "is lexically equal to cat"

if animal != 'cat' then

 say animal "is not lexically equal cat"

if animal > 'cat' then

 say animal "is lexically higher than cat"

if animal < 'cat' then

 say animal "is lexically lower than cat"

if animal >= 'cat' then

 say animal "is not lexically lower than cat"

if animal <= 'cat' then

 say animal "is not lexically higher than cat"

/* The above comparative operators do not consider

  leading and trailing whitespace when making comparisons. */

if ' cat ' = 'cat' then

 say "this will print because whitespace is stripped"

/* To consider all whitespace in a comparison

  we need to use strict comparative operators */

if ' cat ' == 'cat' then

 say "this will not print because comparison is strict"</lang>

Here is a list of the strict comparative operators and their meaning:

  • == Strictly Equal To
  • !\== Strictly Not Equal To
  • << Strictly Less Than
  • >> Strictly Greater Than
  • <<= Strictly Less Than or Equal To
  • >>= Strictly Greater Than or Equal To
  • !\<< Strictly Not Less Than
  • !\>> Strictly Not Greater Than

Tcl

The best way to compare two strings in Tcl for equality is with the eq and ne expression operators: <lang tcl>if {$a eq $b} {

   puts "the strings are equal"

} if {$a ne $b} {

   puts "the strings are not equal"

}</lang> The numeric == and != operators also mostly work, but can give somewhat unexpected results when the both the values look numeric. The string equal command is equally suited to equality-testing (and generates the same bytecode).

For ordering, the < and > operators may be used, but again they are principally numeric operators. For guaranteed string ordering, the result of the string compare command should be used instead (which uses the unicode codepoints of the string): <lang tcl>if {[string compare $a $b] < 0} {

   puts "first string lower than second"

} if {[string compare $a $b] > 0} {

   puts "first string higher than second"

}</lang> Greater-or-equal and less-or-equal operations can be done by changing what exact comparison is used on the result of the string compare.

Tcl also can do a prefix-equal (approximately the same as strncmp() in C) through the use of the -length option: <lang tcl>if {[string equal -length 3 $x "abc123"]} {

   puts "first three characters are equal"

}</lang> And case-insensitive equality is (orthogonally) enabled through the -nocase option. These options are supported by both string equal and string compare, but not by the expression operators.

UNIX Shell

<lang sh>#!/bin/sh

A="Bell" B="Ball"

  1. Traditional test command implementations test for equality and inequality
  2. 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>