3.3 Strings
Strings (Unicode) in Guide: Racket 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 also: immutable?, symbol->string, bytes->string/utf-8.
3.3.1 String Constructors, Selectors, and Mutators
Examples: |
> (string? "Apple") |
#t |
> (string? 'apple) |
#f |
(make-string k [char]) → string? |
k : exact-nonnegative-integer? |
char : char? = #\nul |
Example: |
> (make-string 5 #\z) |
"zzzzz" |
Example: |
> (string #\A #\p #\p #\l #\e) |
"Apple" |
(string->immutable-string str) → (and/c string? immutable?) |
str : string? |
(string-length str) → exact-nonnegative-integer? |
str : string? |
Example: |
> (string-length "Apple") |
5 |
(string-ref str k) → char? |
str : string? |
k : exact-nonnegative-integer? |
Example: |
> (string-ref "Apple" 0) |
#\A |
(string-set! str k char) → void? |
str : (and/c string? (not/c immutable?)) |
k : exact-nonnegative-integer? |
char : char? |
Examples: |
> (define s (string #\A #\p #\p #\l #\e)) |
> (string-set! s 4 #\y) |
> s |
"Apply" |
(substring str start [end]) → string? |
str : string? |
start : exact-nonnegative-integer? |
end : exact-nonnegative-integer? = (string-length str) |
Examples: |
> (substring "Apple" 1 3) |
"pp" |
> (substring "Apple" 1) |
"pple" |
(string-copy str) → string? |
str : string? |
| |||||||||||||||||||||||||||||||||||
dest : (and/c string? (not/c immutable?)) | |||||||||||||||||||||||||||||||||||
dest-start : exact-nonnegative-integer? | |||||||||||||||||||||||||||||||||||
src : string? | |||||||||||||||||||||||||||||||||||
src-start : exact-nonnegative-integer? = 0 | |||||||||||||||||||||||||||||||||||
src-end : exact-nonnegative-integer? = (string-length src) |
Examples: |
> (define s (string #\A #\p #\p #\l #\e)) |
> (string-copy! s 4 "y") |
> (string-copy! s 0 s 3 4) |
> s |
"lpply" |
(string-fill! dest char) → void? |
dest : (and/c string? (not/c immutable?)) |
char : char? |
Examples: |
> (define s (string #\A #\p #\p #\l #\e)) |
> (string-fill! s #\q) |
> s |
"qqqqq" |
(string-append str ...) → string? |
str : string? |
Example: |
> (string-append "Apple" "Banana") |
"AppleBanana" |
(string->list str) → (listof char?) |
str : string? |
Example: |
> (string->list "Apple") |
'(#\A #\p #\p #\l #\e) |
(list->string lst) → string? |
lst : (listof char?) |
Example: |
> (list->string (list #\A #\p #\p #\l #\e)) |
"Apple" |
(build-string n proc) → string? |
n : exact-nonnegative-integer? |
proc : (exact-nonnegative-integer? . -> . char?) |
Example: |
> (build-string 5 (lambda (i) (integer->char (+ i 97)))) |
"abcde" |
3.3.2 String Comparisons
Examples: |
> (string=? "Apple" "apple") |
#f |
> (string=? "a" "as" "a") |
#f |
Examples: |
> (string<? "Apple" "apple") |
#t |
> (string<? "apple" "Apple") |
#f |
> (string<? "a" "b" "c") |
#t |
Examples: |
> (string<=? "Apple" "apple") |
#t |
> (string<=? "apple" "Apple") |
#f |
> (string<=? "a" "b" "b") |
#t |
Examples: |
> (string>? "Apple" "apple") |
#f |
> (string>? "apple" "Apple") |
#t |
> (string>? "c" "b" "a") |
#t |
Examples: |
> (string>=? "Apple" "apple") |
#f |
> (string>=? "apple" "Apple") |
#t |
> (string>=? "c" "b" "b") |
#t |
(string-ci=? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
Examples: |
> (string-ci=? "Apple" "apple") |
#t |
> (string-ci=? "a" "a" "a") |
#t |
(string-ci<? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
Examples: |
> (string-ci<? "Apple" "apple") |
#f |
> (string-ci<? "apple" "banana") |
#t |
> (string-ci<? "a" "b" "c") |
#t |
(string-ci<=? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
Examples: |
> (string-ci<=? "Apple" "apple") |
#t |
> (string-ci<=? "apple" "Apple") |
#t |
> (string-ci<=? "a" "b" "b") |
#t |
(string-ci>? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
Examples: |
> (string-ci>? "Apple" "apple") |
#f |
> (string-ci>? "banana" "Apple") |
#t |
> (string-ci>? "c" "b" "a") |
#t |
(string-ci>=? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
Examples: |
> (string-ci>=? "Apple" "apple") |
#t |
> (string-ci>=? "apple" "Apple") |
#t |
> (string-ci>=? "c" "b" "b") |
#t |
3.3.3 String Conversions
(string-upcase str) → string? |
str : string? |
Examples: |
> (string-upcase "abc!") |
"ABC!" |
> (string-upcase "Straße") |
"STRASSE" |
(string-downcase string) → string? |
string : string? |
Examples: |
> (string-downcase "aBC!") |
"abc!" |
> (string-downcase "Straße") |
"straße" |
> (string-downcase "ΚΑΟΣ") |
"καος" |
> (string-downcase "Σ") |
"σ" |
(string-titlecase string) → string? |
string : string? |
Examples: |
> (string-titlecase "aBC twO") |
"Abc Two" |
> (string-titlecase "y2k") |
"Y2K" |
> (string-titlecase "main straße") |
"Main Straße" |
> (string-titlecase "stra ße") |
"Stra Sse" |
(string-foldcase string) → string? |
string : string? |
Examples: |
> (string-foldcase "aBC!") |
"abc!" |
> (string-foldcase "Straße") |
"strasse" |
> (string-foldcase "ΚΑΟΣ") |
"καοσ" |
(string-normalize-nfd string) → string? |
string : string? |
(string-normalize-nfkd string) → string? |
string : string? |
(string-normalize-nfc string) → string? |
string : string? |
(string-normalize-nfkc string) → string? |
string : string? |
3.3.4 Locale-Specific String Operations
(string-locale=? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
(string-locale<? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
(string-locale>? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
(string-locale-ci=? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
(string-locale-ci<? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
(string-locale-ci>? str1 str2 ...+) → boolean? |
str1 : string? |
str2 : string? |
(string-locale-upcase string) → string? |
string : string? |
(string-locale-downcase string) → string? |
string : string? |
3.3.5 Additional String Functions
(string-append* str ... strs) → string? |
str : string? |
strs : (listof string?) |
Examples: | ||
> (string-append* "a" "b" '("c" "d")) | ||
"abcd" | ||
| ||
"Alpha, Beta, Gamma" |
(string-join strs sep) → string? |
strs : (listof string?) |
sep : string? |
Example: |
> (string-join '("one" "two" "three" "four") " potato ") |
"one potato two potato three potato four" |