On this page:
String
Readable  String
Readable  String.to_  string
Mutable  String
to_  string
repr
+  &
String.append
String.make
String.length
String.get
String.substring
String.find
String.contains
String.starts_  with
String.ends_  with
String.replace
String.join
String.split
String.trim
String.utf8_  bytes
String.latin1_  bytes
String.locale_  bytes
String.to_  int
String.to_  number
String.to_  string
Readable  String.to_  string
String.upcase
String.downcase
String.foldcase
String.titlecase
String.locale_  upcase
String.locale_  downcase
String.normalize_  nfd
String.normalize_  nfkd
String.normalize_  nfc
String.normalize_  nfkc
String.grapheme_  span
String.grapheme_  count
String.to_  sequence
String.copy
String.snapshot
String  CI
Readable  String  CI
String  Locale
Readable  String  Locale
String  Locale  CI
Readable  String  Locale  CI
8.15.0.12

7.4 Strings🔗ℹ

A string is a sequence of Unicode characters. A string works with map-referencing [] to access a character via #%index. A string also works with the ++ operator to append strings, but a +& can be used to append strings with the static guarantee that the result is a string. A string can be used as sequence, in which case it supplies its characters in order.

Although Racket supports mutable strings, the String annotation recognizes only immutable strings, and Rhombus operations generate immutable strings. Some operations allow mutable strings as input, and ReadableString recognizes both mutable and immutable strings.

Two strings are equal by is_now as long as they have equal contents, even if one is mutable and the other is immutable.

Strings are comparable, which means that generic operations like < and > work on strings.

annotation

String

 

annotation

ReadableString

 

annotation

ReadableString.to_string

 

annotation

MutableString

Matches strings. The ReadableString annotation allows mutable Racket strings as well as immutable Rhombus strings, while MutableString matches only mutable strings. The ReadableString.to_string converter annotation allows the same strings as ReadableString, but converts a mutable Racket string to an immutable Rhombus string, like String.snapshot.

Static information associated by String, etc., makes an expression acceptable as a sequence to for in static mode.

function

fun to_string(v :: Any,

              ~mode: mode :: PrintMode = #'text) :: String

 

function

fun repr(v :: Any) :: String

The to_string function coerces v to a string.

The string form of a value corresponds to the way that print would print, which means that strings, symbols, identifiers, and keywords convert as their character content in the default #'text mode.

The repr function is a shorthand for to_string with ~mode: #'expr.

For converting a syntax object to a string, see also Syntax.to_source_string.

> to_string(10)

"10"

> to_string('hello')

"hello"

> to_string([1, 2, 3])

"[1, 2, 3]"

> to_string('hello', ~mode: #'expr)

"'hello'"

operator

operator ((v1 :: Any) +& (v2 :: Any)) :: String:

  ~order: concatenation

Coerces v1 and v2 to a string, then appends the strings.

The value is coerced to a string in the same way as by to_string.

> "hello" +& "world"

"helloworld"

> "it goes to " +& 11

"it goes to 11"

> "the list " +& [1, 2, 3] +& " has " +& 3 +& " elements"

"the list [1, 2, 3] has 3 elements"

Appends all strs to create a new string.

> String.append()

""

> String.append("this")

"this"

> String.append("this", " and ", "that")

"this and that"

function

fun String.make(len :: NonnegInt, c :: Char) :: String

Creates a string of length n where every position in the string contains c.

> String.make(5, Char"x")

"xxxxx"

Returns the number of characters in str.

> String.length("hello")

5

> "hello".length()

5

Equivalent to str[n] (with the default implicit #%index form). Returns the nth character of str (starting from 0).

> "abc"[0] +& "abc".get(0)

"aa"

method

method String.substring(str :: ReadableString,

                        start :: NonnegInt,

                        end :: NonnegInt = String.length(str))

  :: String

Returns the substring of str from start (inclusive) to end (exclusive).

> String.substring("hello", 2, 4)

"ll"

> String.substring("hello", 2)

"llo"

method

method String.find(str :: ReadableString,

                   substr :: ReadableString)

  :: maybe(NonnegInt)

 

method

method String.contains(str :: ReadableString,

                       substr :: ReadableString)

  :: Boolean

 

method

method String.starts_with(str :: ReadableString,

                          substr :: ReadableString)

  :: Boolean

 

method

method String.ends_with(str :: ReadableString,

                        substr :: ReadableString)

  :: Boolean

Checks whether str contains substr as a substring. The String.find function reports the first position in str where substr starts, if substr is found. The String.starts_with and String.ends_with functions return #true only when substr is at the start or end of str, respectively.

> String.contains("howdy", "how")

#true

> "howdy".contains("how")

#true

> "howdy".contains("nope")

#false

> "say howdy".find("how")

4

> "say howdy".find("nope")

#false

> "say howdy".starts_with("say")

#true

> "say howdy".ends_with("dy")

#true

method

method String.replace(str :: ReadableString,

                      from :: ReadableString || RX,

                      to :: ReadableString,

                      ~all: all = #false)

  :: String

Replaces either the first or every (depending on the all argument) non-overlapping instance of from in str with to.

> String.replace("Hello", "l", "x")

"Hexlo"

> String.replace("Hello", "l", "x", ~all: #true)

"Hexxo"

function

fun String.join(

  strs :: List.of(ReadableString),

  sep :: ReadableString = " ",

  ~before_last: before_last :: ReadableString = sep

) :: String

Appends the strings in strs with sep in between, except that before_last is used between the next-to-last and last string when strs has more than one element.

> String.join(["Hello", "World"])

"Hello World"

> String.join(["lions", "tigers", "bears"], ", ", ~before_last: ", and ")

"lions, tigers, and bears"

method

method String.split(str :: ReadableString,

                    sep :: ReadableString || RX = rx'space+',

                    ~trim: trim = #true,

                    ~repeat: repeat = #false)

  :: List.of(String)

Finds non-overlapping instances of sep in str and returns a list of substrings that appear between the sep instances.

If trim is true, then the result list never starts or ends with an empty string. Otherwise, an instance of sep at the start or end of str implies an empty-string result.

The repeat argument is relevant only when sep is a string instead of a regexp. When repeat is true, then sep is converted to a pattern that matches one or more consecutive instances of sep.

> "Hello  World".split()

["Hello", "World"]

> "Hello  World".split(" ")

["Hello", "", "World"]

> "Hello  World".split(" ", ~repeat: #true)

["Hello", "World"]

> "Hello  World".split(rx'upper')

["ello  ", "orld"]

> "Hello  World".split(rx'upper', ~trim: #false)

["", "ello  ", "orld"]

method

method String.trim(str :: ReadableString,

                   sep :: ReadableString || RX = rx'space+',

                   ~start: start = #true,

                   ~end: end = #true,

                   ~repeat: repeat = #false)

  :: List.of(String)

Removes sep from the start (when start is true) and end (when end is true) of str.

The repeat argument is relevant only when sep is a string instead of a regexp. When repeat is true, then sep is converted to a pattern that matches one or more consecutive instances of sep.

> "  Hello World  ".trim()

"Hello World"

> "  Hello World  ".trim(~start: #false)

"  Hello World"

> "  Hello World  ".trim(~end: #false)

"Hello World  "

> "_Hello World__".trim("_")

"Hello World_"

> "_Hello World__".trim("_", ~repeat: #true)

"Hello World"

method

method String.utf8_bytes(str :: ReadableString,

                         err_byte :: maybe(Byte) = #false,

                         start :: NonnegInt = 0,

                         end :: NonnegInt = String.length(str))

  :: Bytes

 

method

method String.latin1_bytes(str :: ReadableString,

                           err_byte :: maybe(Byte) = #false,

                           start :: NonnegInt = 0,

                           end :: NonnegInt = String.length(str))

  :: Bytes

 

method

method String.locale_bytes(str :: ReadableString,

                           err_byte :: maybe(Byte) = #false,

                           start :: NonnegInt = 0,

                           end :: NonnegInt = String.length(str))

  :: Bytes

Converts a string to a byte string, encoding by UTF-8, Latin-1, or the current locale’s encoding. The err_byte argument provides a byte to use in place of an encoding error, where #false means that an exception is thrown. (No encoding error is possible with String.utf8_bytes, but err_byte is accepted for consistency.)

> "hello".utf8_bytes()

#"hello"

Parses str as an integer, returning #false if the string does not parse as an integer, otherwise returning the integer value.

> String.to_int("-42")

-42

> String.to_int("42.0")

#false

> String.to_int("forty-two")

#false

> "100".to_int()

100

Parses str as a number, returning #false if the string does not parse as a number, otherwise returning the number value.

> String.to_number("-42")

-42

> String.to_number("42.0")

42.0

> String.to_number("forty-two")

#false

> "3/4".to_number()

3/4

The same as to_string, but constrained to a ReadableString argument. In other words, these equivalent to String.snapshot. The ReadableString.to_string function exists for consistency with the ReadableString.to_string annotation.

Case-conversion functions. The locale_upcase and locale_downcase functions are sensitive to the current locale, but the other functions are locale-independent conversions defined by the Unicode standard.

Unicode normalization functions.

method

method String.grapheme_span(str :: ReadableString,

                            start :: NonnegInt = 0,

                            end :: NonnegInt = String.length(str))

  :: NonnegInt

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

method

method String.grapheme_count(str :: ReadableString,

                             start :: NonnegInt = 0,

                             end :: NonnegInt = String.length(str))

  :: NonnegInt

Returns the number of grapheme clusters in String.substring(str, start, end).

The start and end arguments must be valid indices as for String.substring.

Implements Sequenceable by returning a sequence of str’s characters in order.

Creates a mutable copy of str.

> def s = "apple"

> s.copy()

String.copy("apple")

> s.copy() is_now s

#true

Returns an immutable string as-is or copies a mutable string’s content to an immutable string.

> def s = "apple"

> s.snapshot()

"apple"

> s.snapshot() === s

#true

> def s = "apple".copy()

> s.snapshot()

"apple"

> s.snapshot() is_now s

#true

annotation

StringCI

 

annotation

ReadableStringCI

A veneer for a string that redirects comparable operations like < and > to case-insensitive comparisons, equivalent to using String.foldcase on each string before comparing.

As always for a veneer, StringCI and ReadableStringCI work only in static mode (see use_static) to help ensure that they have the intended effect.

> "apple" < "BANANA"

#false

> ("apple" :: StringCI) < ("BANANA" :: StringCI)

#true

annotation

StringLocale

 

annotation

ReadableStringLocale

 

annotation

StringLocaleCI

 

annotation

ReadableStringLocaleCI

Like StringCI and ReadableStringCI, but for locale-sensitive case-sensitive and case-insensitive comparisons.