On this page:
String
Readable  String
Readable  String.to_  string
to_  string
+  &
String.append
String.length
String.contains
String.get
String.substring
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.normalize_  nfd
String.normalize_  nfkd
String.normalize_  nfc
String.normalize_  nfkc
String.grapheme_  span
String.grapheme_  count
String  CI
Readable  String  CI
8.12

6.26 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 guaratee that the result is a string. A string can be used as sequence, in which case it supplies its bytes 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.

The . operator can be used on a string expression as equivalent to calling String functions:

str.append(str2, ...)

 is 

String.append(str, str2, ...)

str.length()

 is 

String.length(str)

str.get(n)

 is 

String.get(str, n)

str.substring(arg, ...)

 is 

String.substring(str, arg, ...)

str.contains(substr)

 is 

String.contains(str, substr)

str.utf8_bytes(arg, ...)

 is 

String.utf8_bytes(str, arg, ...)

str.latin1_bytes(arg, ...)

 is 

String.latin1_bytes(str, arg, ...)

str.locale_bytes(arg, ...)

 is 

String.locale_bytes(str, arg, ...)

str.to_int()

 is 

String.to_int(str)

str.to_number()

 is 

String.to_number(str)

str.to_string()

 is 

String.to_string(str)

str.upcase(arg)

 is 

String.upcase(str, arg)

str.downcase(arg)

 is 

String.downcase(str, arg)

str.foldcase(arg)

 is 

String.foldcase(str, arg)

str.titlecase(arg)

 is 

String.titlecase(str, arg)

str.normalize_nfd()

 is 

String.normalize_nfd(str)

str.normalize_nfkd()

 is 

String.normalize_nfkd(str)

str.normalize_nfc()

 is 

String.normalize_nfc(str)

str.normalize_nfkc()

 is 

String.normalize_nfkc(str)

str.grapheme_span(arg, ...)

 is 

String.grapheme_span(str, arg, ...)

str.grapheme_count(arg, ...)

 is 

String.grapheme_count(str, arg, ...)

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

annotation

String

 

annotation

ReadableString

 

annotation

ReadableString.to_string

Matches strings. The ReadableString annotation allows mutable Racket strings as well as immutable Rhombus strings. The ReadableString.to_string converter annotation allows the same strings as ReadableString, but converts a mutable Racket string to an immutable Rhombus string.

function

fun to_string(v :: Any,

              ~mode: mode :: Any.of(#'text, #'expr) = #'text)

  :: String

Coerces v to a string.

The string for 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.

> 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

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"

function

fun String.append(str :: ReadableString, ...) :: String

Appends all strs to create a new string.

> String.append()

""

> String.append("this")

"this"

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

"this and that"

Returns the number of characters in str.

> String.length("hello")

5

> "hello".length()

5

function

fun String.contains(str :: ReadableString,

                    substr :: ReadableString)

  :: Boolean

Checks whether str contains substr as a substring.

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

#true

> "howdy".contains("how")

#true

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

#false

> "howdy".contains("nope")

#false

function

fun String.get(str :: ReadableString, n :: NonnegInt) :: Char

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"

function

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

function

fun String.utf8_bytes(str :: ReadableString,

                      err_byte :: maybe(Byte) = #false,

                      start :: NonnegInt = 0,

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

  :: Bytes

 

function

fun String.latin1_bytes(str :: ReadableString,

                        err_byte :: maybe(Byte) = #false,

                        start :: NonnegInt = 0,

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

  :: Bytes

 

function

fun 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("fourty-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("fourty-two")

#false

> "3/4".to_number()

3/4

The same as to_string, but constrained to a ReadableString argument. These functions exist for consistency with the ReadableString.to_string annotation.

Case-conversion functions.

Unicode normalization functions.

function

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

function

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

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 normally should be used in static mode to help ensure that they have the intended effect.

> "apple" < "BANANA"

#false

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

#true