Next: , Previous: , Up: srfi strings spec   [Index]


2.8.3.9 Searching

The following functions scan a string for the first character which:

The start and end arguments specify the beginning and end indices of the search; the search includes the start index, but not the end index. Be careful of “fencepost” considerations: when searching right–to–left, the first index considered is end-1, whereas when searching left–to–right, the first index considered is start.

That is, the start/end indices describe a same half–open interval [start, end) in these procedures that they do in all the other SRFI-13 procedures.

Function: string-index str char/char-set/pred
Function: string-index str char/char-set/pred start
Function: string-index str char/char-set/pred start end

Search through the string from the left, returning the index of the first occurrence of a character matching the criterion; if no match is found: return #f.

Function: string-index-right str char/char-set/pred
Function: string-index-right str char/char-set/pred start
Function: string-index-right str char/char-set/pred start end

Search through the string from the right, returning the index of the first occurrence of a character matching the criterion; if no match is found: return #f.

Function: string-skip str char/char-set/pred
Function: string-skip str char/char-set/pred start
Function: string-skip str char/char-set/pred start end

Search the string from the left, returning the index of the first char that does not satisfy the criterion; if no match is found: return #f.

E.g., to skip over initial whitespace, say:

(cond ((string-skip s char-set:whitespace) =>
       (lambda (i) ...)) ;s[i] is not whitespace
      ...)
Function: string-skip-right str char/char-set/pred
Function: string-skip-right str char/char-set/pred start
Function: string-skip-right str char/char-set/pred start end

Search the string from the right, returning the index of the first char that does not satisfy the criterion; if no match is found: return #f.

Function: string-count str char/char-set/pred
Function: string-count str char/char-set/pred start
Function: string-count str char/char-set/pred start end

Return a count of the number of characters in str that satisfy the char/char-set/pred argument. If this argument is a procedure, it is applied to the character as a predicate; if it is a character set, the character is tested for membership; if it is a character, it is used in an equality test.

Function: string-contains str1 str2
Function: string-contains str1 str2 start1
Function: string-contains str1 str2 start1 end1
Function: string-contains str1 str2 start1 end1 start2
Function: string-contains str1 str2 start1 end1 start2 end2

Return true if the string str1 contains string str2; otherwise return #f. Return the index in str1 where str2 occurs as a substring. The optional start/end indices restrict the operation to the indicated substrings.

The returned index is in the range [start1, end1). A successful match must lie entirely in the [start1, end1) range of str1.

Example:

;; Searches "a geek"
(string-contains "eek -- what a geek." "ee" 12 18)
⇒ 15
Function: string-contains-ci str1 str2
Function: string-contains-ci str1 str2 start1
Function: string-contains-ci str1 str2 start1 end1
Function: string-contains-ci str1 str2 start1 end1 start2
Function: string-contains-ci str1 str2 start1 end1 start2 end2

Case–insensitive variant.


Next: , Previous: , Up: srfi strings spec   [Index]