A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://docs.racket-lang.org/reference/strings.html below:

4.4 Strings

4.4 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. 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 reading strings and Printing Strings for information on printing strings.

See also: immutable?, symbol->string, bytes->string/utf-8.

4.4.1 String Constructors, Selectors, and Mutators🔗ℹ

Returns #t if v is a string, #f otherwise.

See also immutable-string? and mutable-string?.

Examples:

Returns a new mutable string of length k where each position in the string is initialized with the character char.

Example:

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.

Examples:

Returns the length of str.

Example:

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.

Example:

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.

Examples:

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 first position in a string corresponds to

0

, so 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.

Examples:

Changes the characters of

dest

starting at position

dest-start

to match the characters in

src

from

src-start

(inclusive) to

src-end

(exclusive), where the first position in a string corresponds to

0

. 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.

Examples:

Changes dest so that every position in the string is filled with char.

Examples:

Returns a new mutable string that is as long as the sum of the given

str

s’ lengths, and that contains the concatenated characters of the given

str

s. If no

str

s are provided, the result is a zero-length string.

Example:

The same as

string-append

, but the result is an immutable string.

Examples:

Added in version 7.5.0.14 of package base.

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.

Example:

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.

Example:

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)

.

Example:

4.4.2 String Comparisons🔗ℹ

Returns

#t

if all of the arguments are

equal?

.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Returns

#t

if the arguments are lexicographically sorted increasing, where individual characters are ordered by

char<?

,

#f

otherwise.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Like

string<?

, but checks whether the arguments are nondecreasing.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Like

string<?

, but checks whether the arguments are decreasing.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Like

string<?

, but checks whether the arguments are nonincreasing.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Returns

#t

if all of the arguments are

equal?

after locale-insensitive case-folding via

string-foldcase

.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

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).

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Like

string-ci<?

, but checks whether the arguments would be nondecreasing after case-folding.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Like

string-ci<?

, but checks whether the arguments would be decreasing after case-folding.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

Like

string-ci<?

, but checks whether the arguments would be nonincreasing after case-folding.

Examples:

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

4.4.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.

Examples:

Examples:

Like

string-upcase

, but the titlecase conversion only for the first character in each sequence of cased characters in

str

(ignoring case-ignorable characters).

Examples:

Examples:

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).

Example:

Example:

Example:

Example:

4.4.4 Locale-Specific String Operations🔗ℹ

Changed in version 7.0.0.13 of package base: Allow one argument, in addition to allowing two or more.

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.

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.

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).

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.

4.4.5 String Grapheme Clusters🔗ℹ

Returns the number of characters (i.e., code points) in the string that form a Unicode grapheme cluster starting at start, assuming that start is the start of a grapheme cluster and extending no further than the character before end. The result is 0 if start equals end.

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.

Examples:

Added in version 8.6.0.2 of package base.

Returns the number of grapheme clusters in

(substring str start end)

.

The start and end arguments must be valid indices as for substring, otherwise the exn:fail:contract exception is raised.

Examples:

Added in version 8.6.0.2 of package base.

4.4.6 Additional String Functions🔗ℹ

Examples:

Appends the strings in

strs

, inserting

sep

between each pair of strings in

strs

.

before-last

,

before-first

, and

after-last

are analogous to the inputs of

add-between

: they specify an alternate separator between the last two strings, a prefix string, and a suffix string respectively.

Examples:

> (string-join '("one" "two" "three" "four"))

"one two three four"

> (string-join '("one" "two" "three" "four") ", ")

"one, two, three, four"

> (string-join '("one" "two" "three" "four") " potato ")

"one potato two potato three potato four"

> (string-join '("x" "y" "z") ", "                #:before-first "Todo: "                #:before-last " and "                #:after-last ".")

"Todo: x, y and z."

Normalizes spaces in the input

str

by trimming it (using

string-trim

and

sep

) and replacing all whitespace sequences in the result with

space

, which defaults to a single space.

Example:

The result of (string-normalize-spaces str sep space) is the same as (string-join (string-split str sep ....) space).

Returns

str

with all occurrences of

from

replaced with by

to

. If

from

is a string, it is matched literally (as opposed to being used as a

regular expression

).

By default, all occurrences are replaced, but only the first match is replaced if all? is #f.

Example:

Splits the input

str

on

sep

, returning a list of substrings of

str

that are separated by

sep

, defaulting to splitting the input on whitespaces. The input is first trimmed using

sep

(see

string-trim

), unless

trim?

is

#f

. Empty matches are handled in the same way as for

regexp-split

. As a special case, if

str

is the empty string after trimming, the result is

'()

instead of

'("")

.

Like string-trim, provide sep to use a different separator, and repeat? controls matching repeated sequences.

Examples:

Trims the input

str

by removing prefix and suffix

sep

, which defaults to whitespace. A string

sep

is matched literally (as opposed to being used as a

regular expression

).

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:

> (string-trim "  foo bar  baz \r\n\t")

"foo bar  baz"

> (string-trim "  foo bar  baz \r\n\t" " " #:repeat? #t)

"foo bar  baz \r\n\t"

> (string-trim "aaaxaayaa" "aa")

"axaay"

Returns #t if x is a string and is not empty; returns #f otherwise.

Added in version 6.3 of package base.

Checks whether

s

includes at any location, starts with, or ends with the second argument, respectively. The

string-find

function returns the first position within

s

where

contained

is found, if any, while

string-contains?

reports only whether it was found.

Examples:

Added in version 6.3 of package base.
Changed in version 8.15.0.7: Added string-find.

4.4.7 Converting Values to Strings🔗ℹ

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.

Converts each

v

to a string in

display

mode—

that is, like

(format "~a" v)

—

then concatenates the results with

separator

between consecutive items, and then pads or truncates the string to be at least

min-width

characters and at most

max-width

characters.

Examples:

> (~a "north")

"north"

> (~a 'south)

"south"

> (~a #"east")

"east"

> (~a #\w "e" 'st)

"west"

> (~a (list "red" 'green #"blue"))

"(red green blue)"

> (~a 17)

"17"

> (~a #e1e20)

"100000000000000000000"

> (~a pi)

"3.141592653589793"

> (~a (expt 6.1 87))

"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.

Examples:

> (~a "abcde" #:max-width 5)

"abcde"

> (~a "abcde" #:max-width 4)

"abcd"

> (~a "abcde" #:max-width 4 #:limit-marker "*")

"abc*"

> (~a "abcde" #:max-width 4 #:limit-marker "...")

"a..."

> (~a "The quick brown fox" #:max-width 15 #:limit-marker "")

"The quick brown"

> (~a "The quick brown fox" #:max-width 15 #:limit-marker "...")

"The quick br..."

> (~a "The quick brown fox" #:max-width 15 #:limit-marker "..." #:limit-prefix? #f)

"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.

Examples:

> (~a "apple" #:min-width 20 #:align 'left)

"apple               "

> (~a "pear" #:min-width 20 #:align 'left #:right-pad-string " .")

"pear . . . . . . . ."

> (~a "plum" #:min-width 20 #:align 'right #:left-pad-string ". ")

". . . . . . . . plum"

> (~a "orange" #:min-width 20 #:align 'center                 #: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:

> (~a "terse" #:width 6)

"terse "

> (~a "loquacious" #:width 6)

"loquac"

Like

~a

, but each value is converted like

(format "~v" v)

, the default separator is

" "

, and the default limit marker is

"..."

.

Examples:

> (~v "north")

"\"north\""

> (~v 'south)

"'south"

> (~v #"east")

"#\"east\""

> (~v #\w)

"#\\w"

> (~v (list "red" 'green #"blue"))

"'(\"red\" green #\"blue\")"

Use ~v to produce text that talks about Racket values.

Example:

"The even numbers in '(0 1 2 3 4 5 6 7 8 9) are '(0 2 4 6 8)."

Like

~a

, but each value is converted like

(format "~s" v)

, the default separator is

" "

, and the default limit marker is

"..."

.

Examples:

> (~s "north")

"\"north\""

> (~s 'south)

"south"

> (~s #"east")

"#\"east\""

> (~s #\w)

"#\\w"

> (~s (list "red" 'green #"blue"))

"(\"red\" green #\"blue\")"

Like

~a

, but each value is converted like

(format "~e" v)

, the default separator is

" "

, and the default limit marker is

"..."

.

Examples:

> (~e "north")

"\"north\""

> (~e 'south)

"'south"

> (~e #"east")

"#\"east\""

> (~e #\w)

"#\\w"

> (~e (list "red" 'green #"blue"))

"'(\"red\" green #\"blue\")"

Converts the rational number x to a string in either positional or exponential notation, depending on notation. The exactness or inexactness of x does not affect its formatting.

The optional arguments control number formatting:

Changed in version 8.5.0.5 of package base: Added #:groups, #:group-sep and #:decimal-sep.

Like

~a

,

~v

, and

~s

, but each

v

is formatted like

(format "~.a" v)

,

(format "~.v" v)

, and

(format "~.s" v)

, respectively.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4