3.3 Strings
Strings (Unicode) in The Racket Guide introduces strings.
A string is a fixed-length array of
characters.
A string can be mutable or
immutable. When an immutable string is provided to a
procedure like string-set!, the
exn:fail:contract exception is raised. String constants generated by the
default reader (see Reading Strings) are
immutable.
Two strings are equal? when they have the same length and
contain the same sequence of characters.
A string can be used as a single-valued sequence (see
Sequences). The characters of the string serve as elements
of the sequence. See also in-string.
See Reading Strings
for information on reading
strings and Printing Strings
for information on printing strings.
See also: immutable?, symbol->string,
bytes->string/utf-8.
3.3.1 String Constructors, Selectors, and Mutators
Returns #t if v
is a string, #f otherwise.
Returns a new mutable string of length k where
each position in the string is initialized with the character
char.
Returns a new
mutable string whose length is the number of provided chars, and
whose positions are initialized with the given chars.
Example: |
> (string #\A #\p #\p #\l #\e) | "Apple" |
|
Returns an immutable string with the same content as
str, returning str itself if str is
immutable.
Returns the length of str.
Returns the character at position
k in
str.
The first position in the string corresponds to
0, so the
position
k must be less than the length of the string,
otherwise the
exn:fail:contract exception is raised.
Changes the
character position
k in
str to
char. The first
position in the string corresponds to
0, so the position
k must be less than the length of the string, otherwise the
exn:fail:contract exception is raised.
Returns a new mutable string that is
(- end start)
characters long, and that contains the same characters as
str from
start inclusive to
end exclusive.
The
start and
end arguments must be less than or
equal to the length of
str, and
end must be greater
than or equal to
start, otherwise the
exn:fail:contract exception is raised.
Changes the characters of
dest starting at position
dest-start to match the characters in
src from
src-start (inclusive) to
src-end (exclusive). The
strings
dest and
src can be the same string, and in
that case the destination region can overlap with the source region;
the destination characters after the copy match the source characters
from before the copy. If any of
dest-start,
src-start, or
src-end are out of range (taking into
account the sizes of the strings and the source and destination
regions), the
exn:fail:contract exception is raised.
Changes dest so that every position in the
string is filled with char.
Returns a new mutable string that is
as long as the sum of the given
strs’ lengths, and that
contains the concatenated characters of the given
strs. If no
strs are provided, the result is a zero-length string.
Returns a new
list of characters corresponding to the content of
str. That is,
the length of the list is
(string-length str), and the
sequence of characters in
str is the same sequence in the
result list.
Returns a new
mutable string whose content is the list of characters in
lst.
That is, the length of the string is
(length lst), and
the sequence of characters in
lst is the same sequence in
the result string.
Creates a string of
n characters by applying
proc to
the integers from
0 to
(sub1 n) in order. If
str is the resulting string, then
(string-ref str i) is the character produced by
(proc i).
3.3.2 String Comparisons
Returns
#t if all of the arguments are
equal?.
Returns
#t if the arguments are lexicographically sorted
increasing, where individual characters are ordered by
char<?,
#f otherwise.
Like
string<?, but checks whether the arguments are nondecreasing.
Like
string<?, but checks whether the arguments are decreasing.
Like
string<?, but checks whether the arguments are nonincreasing.
Returns
#t if all of the arguments are
eqv? after
locale-insensitive case-folding via
string-foldcase.
Like
string<?, but checks whether the arguments would be in
increasing order if each was first case-folded using
string-foldcase (which is locale-insensitive).
Like
string-ci<?, but checks whether the arguments would be nondecreasing after case-folding.
Like
string-ci<?, but checks whether the arguments would be decreasing after case-folding.
Like
string-ci<?, but checks whether the arguments would be nonincreasing after case-folding.
3.3.3 String Conversions
Returns a string
whose characters are the upcase conversion of the characters in
str. The conversion uses Unicode’s locale-independent
conversion rules that map code-point sequences to code-point
sequences (instead of simply mapping a 1-to-1 function on code points
over the string), so the string produced by the conversion can be
longer than the input string.
Like
string-upcase, but the titlecase conversion only for the
first character in each sequence of cased characters in
str
(ignoring case-ignorable characters).
Returns a
string that is the Unicode normalized form D of string. If
the given string is already in the corresponding Unicode normal form,
the string may be returned directly as the result (instead of a newly
allocated string).
3.3.4 Locale-Specific String Operations
Like
string<?, but the sort order compares strings in a
locale-specific way, based on the value of
current-locale. In
particular, the sort order may not be simply a lexicographic
extension of character ordering.
Like
string-locale=?, but strings are compared
using rules that are both locale-specific and case-insensitive
(depending on what “case-insensitive” means for the current
locale).
3.3.5 Additional String Functions
Appends the strings in strs, inserting sep between
each pair of strings in strs.
Example: |
> (string-join '("one" "two" "three" "four") " potato ") | "one potato two potato three potato four" |
|