SRFI-13 String Library¶
Extended string operations beyond R7RS base. Import with
(import (srfi 13)). For core string procedures, see
Strings.
Searching¶
string-contains¶
Syntax: (string-contains string pattern) | (string-contains string pattern start) | (string-contains string pattern start end)
Searches string for the first occurrence of pattern as a substring. Returns
the index of the beginning of the match, or #f if pattern is not found.
The optional start and end arguments restrict the search to a substring of
string.
kaappi> (string-contains "hello world" "world")
;=> 6
kaappi> (string-contains "hello world" "xyz")
;=> #f
kaappi> (string-contains "abcabc" "bc" 3)
;=> 4
See also: string-index,
string-prefix?
string-index¶
Syntax: (string-index string pred/char/char-set) | (string-index string pred/char/char-set start) | (string-index string pred/char/char-set start end)
Returns the index of the first character in string that satisfies
pred/char/char-set. The criterion can be a predicate procedure, a character,
or a char-set. Returns #f if no matching character is found. The optional
start and end arguments restrict the search to a substring.
kaappi> (string-index "hello world" char-whitespace?)
;=> 5
kaappi> (string-index "hello" #\l)
;=> 2
kaappi> (string-index "abcde" char-numeric?)
;=> #f
See also: string-index-right,
string-skip
string-index-right¶
Syntax: (string-index-right string pred/char/char-set) | (string-index-right string pred/char/char-set start) | (string-index-right string pred/char/char-set start end)
Like string-index, but searches from right to left, returning
the index of the last character that satisfies the criterion. Returns #f if
no matching character is found.
kaappi> (string-index-right "hello world" #\l)
;=> 9
kaappi> (string-index-right "hello" char-upper-case?)
;=> #f
See also: string-index,
string-skip-right
string-skip¶
Syntax: (string-skip string pred/char/char-set) | (string-skip string pred/char/char-set start) | (string-skip string pred/char/char-set start end)
Returns the index of the first character in string that does not satisfy
pred/char/char-set. This is the complement of string-index.
Returns #f if every character matches.
See also: string-skip-right,
string-index
string-skip-right¶
Syntax: (string-skip-right string pred/char/char-set) | (string-skip-right string pred/char/char-set start) | (string-skip-right string pred/char/char-set start end)
Like string-skip, but searches from right to left, returning
the index of the last character that does not satisfy the criterion.
kaappi> (string-skip-right "hello " char-whitespace?)
;=> 4
kaappi> (string-skip-right "aaa" #\a)
;=> #f
See also: string-skip,
string-index-right
string-count¶
Syntax: (string-count string pred/char/char-set) | (string-count string pred/char/char-set start) | (string-count string pred/char/char-set start end)
Returns the number of characters in string that satisfy pred/char/char-set. The optional start and end arguments restrict the count to a substring.
kaappi> (string-count "hello world" char-alphabetic?)
;=> 10
kaappi> (string-count "abracadabra" #\a)
;=> 5
kaappi> (string-count "12345" char-numeric?)
;=> 5
See also: string-index,
string-every
string-prefix?¶
Syntax: (string-prefix? prefix string) | (string-prefix? prefix string start) | (string-prefix? prefix string start end)
Returns #t if prefix is a prefix of string. The optional start and
end arguments restrict the comparison to a substring of string.
kaappi> (string-prefix? "hel" "hello")
;=> #t
kaappi> (string-prefix? "world" "hello")
;=> #f
kaappi> (string-prefix? "" "anything")
;=> #t
See also: string-suffix?,
string-contains
string-suffix?¶
Syntax: (string-suffix? suffix string) | (string-suffix? suffix string start) | (string-suffix? suffix string start end)
Returns #t if suffix is a suffix of string. The optional start and
end arguments restrict the comparison to a substring of string.
kaappi> (string-suffix? "llo" "hello")
;=> #t
kaappi> (string-suffix? "world" "hello")
;=> #f
kaappi> (string-suffix? "" "anything")
;=> #t
See also: string-prefix?,
string-contains
Trimming¶
string-trim¶
Syntax: (string-trim string) | (string-trim string pred/char/char-set) | (string-trim string pred/char/char-set start) | (string-trim string pred/char/char-set start end)
Returns string with leading characters that satisfy pred/char/char-set removed. When called without a criterion, trims whitespace. The optional start and end arguments restrict the operation to a substring.
See also: string-trim-right,
string-trim-both
string-trim-right¶
Syntax: (string-trim-right string) | (string-trim-right string pred/char/char-set) | (string-trim-right string pred/char/char-set start) | (string-trim-right string pred/char/char-set start end)
Returns string with trailing characters that satisfy pred/char/char-set removed. When called without a criterion, trims whitespace.
kaappi> (string-trim-right " hello ")
;=> " hello"
kaappi> (string-trim-right "helloxx" #\x)
;=> "hello"
See also: string-trim,
string-trim-both
string-trim-both¶
Syntax: (string-trim-both string) | (string-trim-both string pred/char/char-set) | (string-trim-both string pred/char/char-set start) | (string-trim-both string pred/char/char-set start end)
Returns string with both leading and trailing characters that satisfy pred/char/char-set removed. When called without a criterion, trims whitespace.
kaappi> (string-trim-both " hello ")
;=> "hello"
kaappi> (string-trim-both "xxhelloxx" #\x)
;=> "hello"
See also: string-trim,
string-trim-right
Splitting and Joining¶
string-split¶
Syntax: (string-split string delimiter)
Splits string at every occurrence of the delimiter string. Returns a list of strings. If the delimiter does not appear, returns a list containing the original string. Adjacent delimiters produce empty strings in the result.
kaappi> (string-split "one,two,three" ",")
;=> ("one" "two" "three")
kaappi> (string-split "hello" ",")
;=> ("hello")
kaappi> (string-split "a::b::c" "::")
;=> ("a" "b" "c")
See also: string-join,
string-contains
string-join¶
Syntax: (string-join list) | (string-join list delimiter) | (string-join list delimiter grammar)
Joins a list of strings into a single string. The optional delimiter defaults to a single space. The optional grammar controls delimiter placement:
'infix(default) -- delimiter between elements'suffix-- delimiter after each element'prefix-- delimiter before each element
kaappi> (string-join '("one" "two" "three"))
;=> "one two three"
kaappi> (string-join '("one" "two" "three") ", ")
;=> "one, two, three"
kaappi> (string-join '("usr" "local" "bin") "/" 'prefix)
;=> "/usr/local/bin"
kaappi> (string-join '("a" "b" "c") ";" 'suffix)
;=> "a;b;c;"
See also: string-split,
string-concatenate
string-concatenate¶
Syntax: (string-concatenate list)
Returns the concatenation of all strings in list. Equivalent to
(apply string-append list) but may be more efficient for large lists.
kaappi> (string-concatenate '("hello" " " "world"))
;=> "hello world"
kaappi> (string-concatenate '())
;=> ""
See also: string-join,
string-append
Selection¶
string-take¶
Syntax: (string-take string nchars)
Returns the first nchars characters of string. It is an error if nchars is greater than the length of string.
See also: string-drop,
string-take-right,
substring
string-drop¶
Syntax: (string-drop string nchars)
Returns string with the first nchars characters removed. It is an error if nchars is greater than the length of string.
See also: string-take,
string-drop-right
string-take-right¶
Syntax: (string-take-right string nchars)
Returns the last nchars characters of string.
kaappi> (string-take-right "hello world" 5)
;=> "world"
kaappi> (string-take-right "hello" 0)
;=> ""
See also: string-drop-right,
string-take
string-drop-right¶
Syntax: (string-drop-right string nchars)
Returns string with the last nchars characters removed.
kaappi> (string-drop-right "hello world" 6)
;=> "hello"
kaappi> (string-drop-right "hello" 0)
;=> "hello"
See also: string-take-right,
string-drop
Padding¶
string-pad¶
Syntax: (string-pad string len) | (string-pad string len char) | (string-pad string len char start) | (string-pad string len char start end)
Returns a string of length len by padding string on the left with char
(default #\space). If string is longer than len, it is truncated on the
left to fit. The optional start and end arguments select a substring before
padding.
kaappi> (string-pad "42" 5)
;=> " 42"
kaappi> (string-pad "42" 5 #\0)
;=> "00042"
kaappi> (string-pad "hello" 3)
;=> "llo"
See also: string-pad-right
string-pad-right¶
Syntax: (string-pad-right string len) | (string-pad-right string len char) | (string-pad-right string len char start) | (string-pad-right string len char start end)
Returns a string of length len by padding string on the right with char
(default #\space). If string is longer than len, it is truncated on the
right to fit.
kaappi> (string-pad-right "hi" 5)
;=> "hi "
kaappi> (string-pad-right "hi" 5 #\.)
;=> "hi..."
kaappi> (string-pad-right "hello" 3)
;=> "hel"
See also: string-pad
Transformation¶
string-reverse¶
Syntax: (string-reverse string) | (string-reverse string start) | (string-reverse string start end)
Returns a newly allocated string whose characters are the reverse of string. The optional start and end arguments restrict the reversal to a substring.
See also: reverse
string-filter¶
Syntax: (string-filter pred/char/char-set string) | (string-filter pred/char/char-set string start) | (string-filter pred/char/char-set string start end)
Returns a string containing only the characters of string that satisfy pred/char/char-set. The optional start and end arguments restrict the operation to a substring.
kaappi> (string-filter char-alphabetic? "h3l1o w0rld")
;=> "hlorld"
kaappi> (string-filter #\a "abracadabra")
;=> "aaaaa"
See also: string-delete,
string-count
string-delete¶
Syntax: (string-delete pred/char/char-set string) | (string-delete pred/char/char-set string start) | (string-delete pred/char/char-set string start end)
Returns a string with all characters of string that satisfy pred/char/char-set
removed. This is the complement of string-filter.
kaappi> (string-delete char-whitespace? "hello world")
;=> "helloworld"
kaappi> (string-delete #\l "hello")
;=> "heo"
See also: string-filter
string-replace¶
Syntax: (string-replace string1 string2 start end)
Returns a new string in which the substring of string1 from codepoint index start (inclusive) to end (exclusive) has been replaced with string2. The replacement string may be a different length than the replaced region.
kaappi> (string-replace "hello world" "there" 6 11)
;=> "hello there"
kaappi> (string-replace "abcde" "XY" 1 4)
;=> "aXYe"
kaappi> (string-replace "abc" "123" 1 1)
;=> "a123bc"
See also: substring,
string-copy!
string-titlecase¶
Syntax: (string-titlecase string) | (string-titlecase string start) | (string-titlecase string start end)
Returns a string with the first letter of each word capitalized and the remaining letters downcased. Word boundaries are determined by transitions from non-letter to letter characters. The optional start and end arguments restrict the operation to a substring.
kaappi> (string-titlecase "hello world")
;=> "Hello World"
kaappi> (string-titlecase "one-two-three")
;=> "One-Two-Three"
See also: string-upcase,
string-downcase
Predicates¶
string-every¶
Syntax: (string-every pred/char/char-set string) | (string-every pred/char/char-set string start) | (string-every pred/char/char-set string start end)
Returns #t if pred/char/char-set is satisfied by every character in
string. Returns #t for the empty string. When pred is a procedure,
returns the value of the last application, or #t if the string is empty.
The optional start and end arguments restrict the test to a substring.
kaappi> (string-every char-alphabetic? "hello")
;=> #t
kaappi> (string-every char-alphabetic? "hello world")
;=> #f
kaappi> (string-every char-numeric? "12345")
;=> #t
kaappi> (string-every char-alphabetic? "")
;=> #t
See also: string-any,
string-count
string-any¶
Syntax: (string-any pred/char/char-set string) | (string-any pred/char/char-set string start) | (string-any pred/char/char-set string start end)
Returns #t if pred/char/char-set is satisfied by at least one character in
string. Returns #f for the empty string. When pred is a procedure,
returns the value of the first successful application. The optional start and
end arguments restrict the test to a substring.
kaappi> (string-any char-numeric? "hello5")
;=> #t
kaappi> (string-any char-numeric? "hello")
;=> #f
kaappi> (string-any char-upper-case? "Hello")
;=> #t
See also: string-every,
string-index
Constructors¶
string-tabulate¶
Syntax: (string-tabulate proc len)
Returns a string of length len where each character is produced by calling proc with the corresponding index (0 to len - 1). proc must return a character.
kaappi> (string-tabulate (lambda (i) (integer->char (+ i 65))) 5)
;=> "ABCDE"
kaappi> (string-tabulate (lambda (i) #\x) 3)
;=> "xxx"
See also: make-string,
list-tabulate
string-unfold¶
Syntax: (string-unfold pred mapper successor seed) | (string-unfold pred mapper successor seed base) | (string-unfold pred mapper successor seed base make-final)
Builds a string by repeatedly applying successor to seed until pred returns true. At each step, mapper is called on the current seed value and must return a character that is appended to the result. The optional base string is prepended to the result. The optional make-final procedure is called on the terminal seed value and its result is appended.
kaappi> (string-unfold (lambda (i) (= i 5))
(lambda (i) (integer->char (+ i 65)))
(lambda (i) (+ i 1))
0)
;=> "ABCDE"
kaappi> (string-unfold null? car cdr '(#\a #\b #\c))
;=> "abc"
See also: string-unfold-right,
string-tabulate
string-unfold-right¶
Syntax: (string-unfold-right pred mapper successor seed) | (string-unfold-right pred mapper successor seed base) | (string-unfold-right pred mapper successor seed base make-final)
Like string-unfold, but builds the string from right to
left. Characters produced by mapper are prepended to the result rather than
appended.
kaappi> (string-unfold-right (lambda (i) (= i 5))
(lambda (i) (integer->char (+ i 65)))
(lambda (i) (+ i 1))
0)
;=> "EDCBA"
kaappi> (string-unfold-right null? car cdr '(#\a #\b #\c))
;=> "cba"
See also: string-unfold,
string-reverse