Next: Matching Strings, Previous: Cutting and Pasting Strings, Up: Strings [Contents][Index]
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.
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
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
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)
⇒ ()
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)
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
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)
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.
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.
Next: Matching Strings, Previous: Cutting and Pasting Strings, Up: Strings [Contents][Index]