Next: , Previous: Selecting String Components, Up: Strings

6.3 Comparison of Strings

— procedure: string=? string1 string2
— procedure: substring=? string1 start end string2 start end
— procedure: string-ci=? string1 string2
— procedure: substring-ci=? string1 start end string2 start end

Returns #t if the two strings (substrings) are the same length and contain the same characters in the same (relative) positions; otherwise returns #f. string-ci=? and substring-ci=? don't distinguish uppercase and lowercase letters, but string=? and substring=? do.

          (string=? "PIE" "PIE")                  =>  #t
          (string=? "PIE" "pie")                  =>  #f
          (string-ci=? "PIE" "pie")               =>  #t
          (substring=? "Alamo" 1 3 "cola" 2 4)    =>  #t ; compares "la"
— procedure: string<? string1 string2
— procedure: substring<? string1 start1 end1 string2 start2 end2
— procedure: string>? string1 string2
— procedure: string<=? string1 string2
— procedure: string>=? string1 string2
— procedure: string-ci<? string1 string2
— procedure: substring-ci<? string1 start1 end1 string2 start2 end2
— procedure: string-ci>? string1 string2
— procedure: string-ci<=? string1 string2
— procedure: string-ci>=? string1 string2

These procedures compare strings (substrings) according to the order of the characters they contain (also see Comparison of Characters). The arguments are compared using a lexicographic (or dictionary) order. If two strings differ in length but are the same up to the length of the shorter string, the shorter string is considered to be less than the longer string.

          (string<? "cat" "dog")          =>  #t
          (string<? "cat" "DOG")          =>  #f
          (string-ci<? "cat" "DOG")       =>  #t
          (string>? "catkin" "cat")       =>  #t ; shorter is lesser
— procedure: string-compare string1 string2 if-eq if-lt if-gt
— procedure: string-compare-ci string1 string2 if-eq if-lt if-gt

If-eq, if-lt, and if-gt are procedures of no arguments (thunks). The two strings are compared; if they are equal, if-eq is applied, if string1 is less than string2, if-lt is applied, else if string1 is greater than string2, if-gt is applied. The value of the procedure is the value of the thunk that is applied.

string-compare distinguishes uppercase and lowercase letters;
string-compare-ci does not.

          (define (cheer) (display "Hooray!"))
          (define (boo)   (display "Boo-hiss!"))
          (string-compare "a" "b"  cheer  (lambda() 'ignore)  boo)
                  -|  Hooray!
                  =>  unspecified
— procedure: string-hash string
— procedure: string-hash-mod string k

string-hash returns an exact non-negative integer that can be used for storing the specified string in a hash table. Equal strings (in the sense of string=?) return equal (=) hash codes, and non-equal but similar strings are usually mapped to distinct hash codes.

string-hash-mod is like string-hash, except that it limits the result to a particular range based on the exact non-negative integer k. The following are equivalent:

          (string-hash-mod string k)
          (modulo (string-hash string) k)