### 6.6 Searching Strings

The first few procedures in this section perform string search, in which a given string (the text) is searched to see if it contains another given string (the pattern) as a proper substring. At present these procedures are implemented using a hybrid strategy. For short patterns of less than 4 characters, the naive string-search algorithm is used. For longer patterns, the Boyer-Moore string-search algorithm is used.

procedure: string-search-forward pattern string
procedure: substring-search-forward pattern string start end

  Pattern must be a string. Searches string for the leftmost occurrence of the substring pattern. If successful, the index of the first character of the matched substring is returned; otherwise, `#f` is returned.

  `substring-search-forward` limits its search to the specified substring of string; `string-search-forward` searches all of string.

      (string-search-forward "rat" "pirate")
          ⇒ 2
      (string-search-forward "rat" "pirate rating")
          ⇒ 2
      (substring-search-forward "rat" "pirate rating" 4 13)
          ⇒ 7
      (substring-search-forward "rat" "pirate rating" 9 13)
          ⇒ #f

procedure: string-search-backward pattern string
procedure: substring-search-backward pattern string start end

  Pattern must be a string. Searches string for the rightmost occurrence of the substring pattern. If successful, the index to the right of the last character of the matched substring is returned; otherwise, `#f` is returned.

  `substring-search-backward` limits its search to the specified substring of string; `string-search-backward` searches all of string.

      (string-search-backward "rat" "pirate")
          ⇒ 5
      (string-search-backward "rat" "pirate rating")
          ⇒ 10
      (substring-search-backward "rat" "pirate rating" 1 8)
          ⇒ 5
      (substring-search-backward "rat" "pirate rating" 9 13)
          ⇒ #f

procedure: string-search-all pattern string
procedure: substring-search-all pattern string start end

  Pattern must be a string. Searches string to find all occurrences of the substring pattern. Returns a list of the occurrences; each element of the list is an index pointing to the first character of an occurrence.

  `substring-search-all` limits its search to the specified substring of string; `string-search-all` searches all of string.

      (string-search-all "rat" "pirate")
          ⇒ (2)
      (string-search-all "rat" "pirate rating")
          ⇒ (2 7)
      (substring-search-all "rat" "pirate rating" 4 13)
          ⇒ (7)
      (substring-search-all "rat" "pirate rating" 9 13)
          ⇒ ()

procedure: substring? pattern string

  Pattern must be a string. Searches string to see if it contains the substring pattern. Returns `#t` if pattern is a substring of string, otherwise returns `#f`.

      (substring? "rat" "pirate")             ⇒  #t
      (substring? "rat" "outrage")            ⇒  #f
      (substring? "" any-string)              ⇒  #t
      (if (substring? "moon" text)
          (process-lunar text)
          'no-moon)

procedure: string-find-next-char string char
procedure: substring-find-next-char string start end char
procedure: string-find-next-char-ci string char
procedure: substring-find-next-char-ci string start end char

  Returns the index of the first occurrence of char in the string (substring); returns `#f` if char does not appear in the string. For the substring procedures, the index returned is relative to the entire string, not just the substring. The `-ci` procedures don’t distinguish uppercase and lowercase letters.

      (string-find-next-char "Adam" #\A)              ⇒  0
      (substring-find-next-char "Adam" 1 4 #\A)       ⇒  #f
      (substring-find-next-char-ci "Adam" 1 4 #\A)    ⇒  2

procedure: string-find-next-char-in-set string char-set
procedure: substring-find-next-char-in-set string start end char-set

  Returns the index of the first character in the string (or substring) that is also in char-set, or returns `#f` if none of the characters in char-set occur in string. For the substring procedure, only the substring is searched, but the index returned is relative to the entire string, not just the substring.

      (string-find-next-char-in-set my-string char-set:alphabetic)
          ⇒  start position of the first word in my-string
      ; Can be used as a predicate:
      (if (string-find-next-char-in-set my-string
                                        (char-set #\( #\) ))
          'contains-parentheses
          'no-parentheses)

procedure: string-find-previous-char string char
procedure: substring-find-previous-char string start end char
procedure: string-find-previous-char-ci string char
procedure: substring-find-previous-char-ci string start end char

  Returns the index of the last occurrence of char in the string (substring); returns `#f` if char doesn’t appear in the string. For the substring procedures, the index returned is relative to the entire string, not just the substring. The `-ci` procedures don’t distinguish uppercase and lowercase letters.

procedure: string-find-previous-char-in-set string char-set
procedure: substring-find-previous-char-in-set string start end char-set

  Returns the index of the last character in the string (substring) that is also in char-set. For the substring procedure, the index returned is relative to the entire string, not just the substring.