30.4 Comparisons of Strings

string1 = string2, string1 < string2

The equality operator = evaluates to true if the two strings string1 and string2 are equal and false otherwise. The inequality operator < returns true if the two strings string1 and string2 are not equal and false otherwise.

    gap> "Hello world.\n" = "Hello world.\n";
    true
    gap> "Hello World.\n" = "Hello world.\n";
    false    # string comparison is case sensitive
    gap> "Hello world." = "Hello world.\n";
    false    # the first string has no <newline>
    gap> "Goodbye world.\n" = "Hello world.\n";
    false
    gap> [ 'a', 'b' ] = "ab";
    true 

string1 < string2, string1 <= string2, string1 string2, string1 = string2

The operators <, <=, , and = evaluate to true if the string string1 is less than, less than or equal to, greater than, greater than or equal to the string string2 respectively. The ordering of strings is lexicographically according to the order implied by the underlying, system dependent, character set.

You can also compare objects of other types, for example integers or permutations with strings. As strings are dense character lists they compare with other objects as lists do, i.e., they are never equal to those objects, records (see Records) are greater than strings, and objects of every other type are smaller than strings.

    gap> "Hello world.\n" < "Hello world.\n";
    false    # the strings are equal
    gap> "Hello World.\n" < "Hello world.\n";
    true    # in ASCII uppercase letters come before lowercase letters
    gap> "Hello world." < "Hello world.\n";
    true    # prefixes are always smaller
    gap> "Goodbye world.\n" < "Hello world.\n";
    true    # 'G' comes before 'H', in ASCII at least 

Previous Up Top Next
Index

GAP 3.4.4
April 1997