+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. Use immutable? to check whether a string is 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 read ing strings and Printing Strings for information on print ing strings.
See also: immutable? , symbol->string , bytes->string/utf-8 .
See also immutable-string? and mutable-string? .
procedure
(make-string k[char])→string?
"zzzzz"
"Apple"
procedure
(string->immutable-string str)→(and/c string? immutable? )
str:string?
#f
#t
procedure
str:string?
5
procedure
(string-ref strk)→char?
str:string?
#\A
procedure
(string-set! strkchar)→void?
char:char?
> s"Apply"
str:string?
procedure
(string-copy str)→string?
str:string?
'("Yui" "Yui")
'("Yui" "Rei")
procedure
dest-startsrc[ src-startdest-start:exact-nonnegative-integer?src:string?
> s"lpply"
procedure
(string-fill! destchar)→void?
char:char?
> s"qqqqq"
procedure
(string-append str...)→string?
str:string?
"AppleBanana"
procedure
(string-append-immutable str...)→(and/c string? immutable? )
str:string?
"AppleBanana"
#t
Added in version 7.5.0.14 of package base.
procedure
(string->list str)→(listof char? )
str:string?
'(#\A #\p #\p #\l #\e)
procedure
(list->string lst)→string?
"Apple"
procedure
( build-string nproc)→string?
"abcde"
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-ci=? str1str2...)→boolean?
str1:string?str2:string?
#t
#t
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-ci<? str1str2...)→boolean?
str1:string?str2:string?
#f
#t
#t
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-ci<=? str1str2...)→boolean?
str1:string?str2:string?
#t
#t
#t
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-ci>? str1str2...)→boolean?
str1:string?str2:string?
#f
#t
#t
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-ci>=? str1str2...)→boolean?
str1:string?str2:string?
#t
#t
#t
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-upcase str)→string?
str:string?
"ABC!"
"STRASSE"
procedure
(string-downcase string)→string?
string:string?
"abc!"
"straße"
"καος"
"σ"
procedure
(string-titlecase string)→string?
string:string?
"AbcTwo"
"Y2k"
"Main Straße"
"Stra Sse"
procedure
(string-foldcase string)→string?
string:string?
"abc!"
"strasse"
"καοσ"
procedure
(string-normalize-nfd string)→string?
string:string?
#t
procedure
(string-normalize-nfkd string)→string?
string:string?
#t
procedure
(string-normalize-nfc string)→string?
string:string?
#t
procedure
(string-normalize-nfkc string)→string?
string:string?
#t
procedure
(string-locale=? str1str2...)→boolean?
str1:string?str2:string?
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-locale<? str1str2...+)→boolean?
str1:string?str2:string?
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-locale>? str1str2...)→boolean?
str1:string?str2:string?
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-locale-ci=? str1str2...)→boolean?
str1:string?str2:string?
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-locale-ci<? str1str2...)→boolean?
str1:string?str2:string?
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-locale-ci>? str1str2...)→boolean?
str1:string?str2:string?
Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.
procedure
(string-locale-upcase string)→string?
string:string?
procedure
(string-locale-downcase string)→string?
string:string?
procedure
(string-grapheme-span strstart[end])→exact-nonnegative-integer?
str:string?
The start and end arguments must be valid indices as for substring , otherwise the exn:fail:contract exception is raised.
See also char-grapheme-cluster-step.
0
1
1
2
2
1
1
Added in version 8.6.0.2 of package base.
procedure
(string-grapheme-count strstart[end])
str:string?
The start and end arguments must be valid indices as for substring , otherwise the exn:fail:contract exception is raised.
0
1
2
2
1
1
3
Added in version 8.6.0.2 of package base.
procedure
( string-append* str...strs)→string?
str:string?
"abcd"
"Alpha, Beta, Gamma"
procedure
[ sep#:before-firstbefore-first#:before-lastbefore-last
"one two three four"
"one, two, three, four"
"one potato two potato three potato four"
"Todo: x, y and z."
procedure
[ sepspace#:trim?trim?str:string?
"foo bar baz"
The result of (string-normalize-spaces strsepspace) is the same as (string-join (string-split strsep....)space).
procedure
( string-replace strfromto[#:all?all?])→string?
str:string?to:string?
By default, all occurrences are replaced, but only the first match is replaced if all? is #f.
"foo blah baz"
procedure
[ sep#:trim?trim?str:string?
Like string-trim , provide sep to use a different separator, and repeat? controls matching repeated sequences.
'("foo" "bar" "baz")
'()
'("" "")
procedure
[ sep#:left?left?#:right?right?str:string?
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 +).
"foo barbaz"
"foo barbaz \r\n\t"
"axaay"
procedure
x:any/c
Added in version 6.3 of package base.
#t
#f
#t
1
#t
Added in version 6.3 of package base.
Changed in version 8.15.0.7: Added string-find .
The racket/format library provides functions for converting Racket values to strings. In addition to features like padding and numeric formatting, the functions have the virtue of being shorter than format (with format string), number->string , or string-append .
procedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/c
"north"
"south"
"east"
"west"
"(red green blue)"
"17"
"100000000000000000000"
"3.141592653589793"
"2.1071509386211452e+68"
The ~a function is primarily useful for strings, numbers, and other atomic data. The ~v and ~s functions are better suited to compound data.
Let s be the concatenated string forms of the vs plus separators. If s is longer than max-width characters, it is truncated to exactly max-width characters. If s is shorter than min-width characters, it is padded to exactly min-width characters. Otherwise s is returned unchanged. If min-width is greater than max-width, an exception is raised.
If s is longer than max-width characters, it is truncated and the end of the string is replaced with limit-marker. If limit-marker is longer than max-width, an exception is raised. If limit-prefix? is #t, the beginning of the string is truncated instead of the end.
"abcde"
"abcd"
"abc*"
"a..."
"The quick brown"
"The quick br..."
"The quick br..."
If s is shorter than min-width, it is padded to at least min-width characters. If align is 'left, then only right padding is added; if align is 'right, then only left padding is added; and if align is 'center, then roughly equal amounts of left padding and right padding are added.
Padding is specified as a non-empty string. Left padding consists of left-pad-string repeated in its entirety as many times as possible followed by a prefix of left-pad-string to fill the remaining space. In contrast, right padding consists of a suffix of right-pad-string followed by a number of copies of right-pad-string in its entirety. Thus left padding starts with the start of left-pad-string and right padding ends with the end of right-pad-string.
"apple "
"pear . . . . . . . ."
". . . . . . . . plum"
#:left-pad-string"- "#:right-pad-string" -")"- - - -orange- - - -"
Use width to set both max-width and min-width simultaneously, ensuring that the resulting string is exactly width characters long:
"terse "
"loquac"
procedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/c
"\"north\""
"'south"
"#\"east\""
"#\\w"
"'(\"red\" green #\"blue\")"
Use ~v to produce text that talks about Racket values.
"The even numbers in '(0 1 2 3 4 5 6 7 8 9) are '(0 2 4 6 8)."
procedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/c
"\"north\""
"south"
"#\"east\""
"#\\w"
"(\"red\" green #\"blue\")"
procedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/c
"\"north\""
"'south"
"#\"east\""
"#\\w"
"'(\"red\" green #\"blue\")"
procedure
[ #:signsign#:basebase#:precisionprecision#:notationnotation#:format-exponentformat-exponent#:min-widthmin-width#:pad-stringpad-string#:groupsgroups#:group-sepgroup-sepsign := #f= 10= 'positional= #f
The optional arguments control number formatting:
notation — determines whether the number is printed in positional or exponential notation. If notation is a function, it is applied to x to get the notation to be used.
precision — controls the number of digits after the decimal point (or more accurately, the radix point). When x is formatted in exponential form, precision applies to the significand.
If precision is a natural number, then up to precision digits are displayed, but trailing zeroes are dropped, and if all digits after the decimal point are dropped the decimal point is also dropped. If precision is (list '=digits), then exactly digits digits after the decimal point are used, and the decimal point is never dropped.
decimal-sep specifies what decimal separator is printed.
groups controls how digits of the integral part of the number are separated into groups. Rightmost numbers of groups are used to group rightmost digits of the integral part. The leftmost number of groups is used repeatedly to group leftmost digits. The group-sep argument specifies which separator to use between digit groups.
min-width — if x would normally be printed with fewer than min-width digits (including the decimal point but not including the sign indicator), the digits are left-padded using pad-string.
pad-string — specifies the string used to pad the number to at least min-width characters (not including the sign indicator). The padding is placed between the sign and the normal digits of x.
If sign is #f (the default), no sign output is generated if x is either positive or zero, and a minus sign is prefixed if x is negative.
If sign is '+, no sign output is generated if x is zero, a plus sign is prefixed if x is positive, and a minus sign is prefixed if x is negative.
If sign is '++, a plus sign is prefixed if x is zero or positive, and a minus sign is prefixed if x is negative.
If sign is 'parens, no sign output is generated if x is zero or positive, and the number is enclosed in parentheses if x is negative.
If sign is (list pos-indzero-indneg-ind), then pos-ind, zero-ind, and neg-ind are used to indicate positive, zero, and negative numbers, respectively. Each indicator is either a string to be used as a prefix or a list containing two strings: a prefix and a suffix.
'("17 up" "an even 0" "42 down")
The default behavior is equivalent to '("""""-"); the 'parens mode is equivalent to '(""""("("")")).
base — controls the base that x is formatted in. If base is a number greater than 10, then lower-case letters are used. If base is (list 'upbase*) and base* is greater than 10, then upper-case letters are used.
format-exponent — determines how the exponent is displayed.
If format-exponent is a string, the exponent is displayed with an explicit sign (as with a sign of '++) and at least two digits, separated from the significand by the “exponent marker” format-exponent:
"1.234E+03"
If format-exponent is #f, the “exponent marker” is "e" if base is 10 and a string involving base otherwise:
"1.234e+03"
"2.322*8^+03"
If format-exponent is a procedure, it is applied to the exponent and the resulting string is appended to the significand:
"1.234E3"
Changed in version 8.5.0.5 of package base: Added #:groups, #:group-sep and #:decimal-sep.
procedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/cprocedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/cprocedure
...[ #:separatorseparator#:widthwidth#:max-widthmax-width#:min-widthmin-width#:limit-markerlimit-marker#:limit-prefix?limit-prefix?#:alignalign#:pad-stringpad-string#:left-pad-stringleft-pad-stringv:any/c