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, and they are interned in read-syntax mode.
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
Examples: | ||||
|
procedure
(make-string k [char]) → string?
k : exact-nonnegative-integer? char : char? = #\nul
Example: | ||
|
Example: | ||
|
procedure
(string->immutable-string str) → (and/c string? immutable?)
str : string?
procedure
str : string?
Example: | ||
|
procedure
(string-ref str k) → char?
str : string? k : exact-nonnegative-integer?
Example: | ||
|
procedure
(string-set! str k char) → void?
str : (and/c string? (not/c immutable?)) k : exact-nonnegative-integer? char : char?
Examples: | ||||||||
|
procedure
str : string? start : exact-nonnegative-integer? end : exact-nonnegative-integer? = (string-length str)
Examples: | ||||
|
procedure
(string-copy str) → string?
str : string?
procedure
(string-copy! dest dest-start src [ src-start src-end]) → void? 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: | |||||||||||
|
procedure
(string-fill! dest char) → void?
dest : (and/c string? (not/c immutable?)) char : char?
Examples: | ||||||||
|
procedure
(string-append str ...) → string?
str : string?
Example: | ||
|
procedure
(string->list str) → (listof char?)
str : string?
Example: | ||
|
procedure
(list->string lst) → string?
lst : (listof char?)
Example: | ||
|
procedure
(build-string n proc) → string?
n : exact-nonnegative-integer? proc : (exact-nonnegative-integer? . -> . char?)
Example: | ||
|
3.3.2 String Comparisons
Examples: | ||||
|
Examples: | ||||||
|
Examples: | ||||||
|
Examples: | ||||||
|
Examples: | ||||||
|
procedure
(string-ci=? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
Examples: | ||||
|
procedure
(string-ci<? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
Examples: | ||||||
|
procedure
(string-ci<=? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
Examples: | ||||||
|
procedure
(string-ci>? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
Examples: | ||||||
|
procedure
(string-ci>=? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
Examples: | ||||||
|
3.3.3 String Conversions
procedure
(string-upcase str) → string?
str : string?
Examples: | ||||
|
procedure
(string-downcase string) → string?
string : string?
Examples: | ||||||||
|
procedure
(string-titlecase string) → string?
string : string?
Examples: | ||||||||
|
procedure
(string-foldcase string) → string?
string : string?
Examples: | ||||||
|
procedure
(string-normalize-nfd string) → string?
string : string?
procedure
(string-normalize-nfkd string) → string?
string : string?
procedure
(string-normalize-nfc string) → string?
string : string?
procedure
(string-normalize-nfkc string) → string?
string : string?
3.3.4 Locale-Specific String Operations
procedure
(string-locale=? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
procedure
(string-locale<? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
procedure
(string-locale>? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
procedure
(string-locale-ci=? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
procedure
(string-locale-ci<? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
procedure
(string-locale-ci>? str1 str2 ...+) → boolean?
str1 : string? str2 : string?
procedure
(string-locale-upcase string) → string?
string : string?
procedure
(string-locale-downcase string) → string?
string : string?
3.3.5 Additional String Functions
procedure
(string-append* str ... strs) → string?
str : string? strs : (listof string?)
Examples: | ||||||
|
procedure
(string-join strs [ sep #:before-first before-first #:before-last before-last #:after-last after-last]) → string? strs : (listof string?) sep : string? = " " before-first : string? = "" before-last : string? = sep after-last : string? = ""
Examples: | ||||||||||||
|
procedure
(string-normalize-spaces str [ sep space #:trim? trim? #:repeat? repeat?]) → string? str : string? sep : (or/c string? regexp?) = #px"\\s+" space : string? = " " trim? : any/c = #t repeat? : any/c = #f
Example: | ||
|
The result of (string-normalize-spaces str sep space) is the same as (string-join (string-split str sep ....) space).
procedure
(string-replace str from to [#:all all?]) → string?
str : string? from : (or/c string? regexp?) to : string? all? : any/c = #t
By default, all occurrences are replaced, but only the first match is replaced if all? is #f.
Example: | ||
|
procedure
(string-split str [ sep #:trim? trim? #:repeat? repeat?]) → (listof string?) str : string? sep : (or/c string? regexp?) = #px"\\s+" trim? : any/c = #t repeat? : any/c = #f
Like string-trim, provide sep to use a different separator, and repeat? controls matching repeated sequences.
Examples: | ||||||
|
procedure
(string-trim str [ sep #:left? left? #:right? right? #:repeat? repeat?]) → string? str : string? sep : (or/c string? regexp?) = #px"\\s+" left? : any/c = #t right? : any/c = #t repeat? : any/c = #f
Use #:left? #f or #:right? #f to suppress trimming the corresponding side. When repeat? is #f (the default), only one match is removed from each side; when repeat? is true, all initial or trailing matches are trimmed (which is an alternative to using a regular expression sep that contains +).
Examples: | ||||||
|