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 | |
| |
annotation | |
| |
annotation | |
| |
annotation | |
Static information associated by String, etc., makes an expression acceptable as a sequence to for in static mode.
function | ||
| ||
function | ||
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]"
"'hello'"
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"
method | |
|
> String.append()
""
> String.append("this")
"this"
> String.append("this", " and ", "that")
"this and that"
> String.make(5, Char"x")
"xxxxx"
method | |
> String.length("hello")
5
5
method | |
|
"aa"
method | ||||
|
> String.substring("hello", 2, 4)
"ll"
> String.substring("hello", 2)
"llo"
method | |||
| |||
| |||
method | |||
| |||
| |||
method | |||
| |||
| |||
method | |||
|
> String.contains("howdy", "how")
#true
#true
#false
4
#false
> "say howdy".starts_with("say")
#true
#true
method | |||||
|
> String.replace("Hello", "l", "x")
"Hexlo"
> String.replace("Hello", "l", "x", ~all: #true)
"Hexxo"
function | |||||
|
> String.join(["Hello", "World"])
"Hello World"
> String.join(["lions", "tigers", "bears"], ", ", ~before_last: ", and ")
"lions, tigers, and bears"
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"]
["Hello", "", "World"]
["Hello", "World"]
["ello ", "orld"]
["", "ello ", "orld"]
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"
" Hello World"
"Hello World "
"Hello World_"
"Hello World"
method | |||||
| |||||
| |||||
method | |||||
| |||||
| |||||
method | |||||
|
> "hello".utf8_bytes()
#"hello"
method | |
|
> String.to_int("-42")
-42
> String.to_int("42.0")
#false
> String.to_int("forty-two")
#false
100
method | |
|
> String.to_number("-42")
-42
> String.to_number("42.0")
42.0
> String.to_number("forty-two")
#false
3/4
method | ||
| ||
function | ||
method | |
| |
method | |
| |
method | |
| |
method | |
| |
method | |
| |
| |
method | |
|
method | |
| |
| |
method | |
| |
| |
method | |
| |
| |
method | |
|
method | ||||
|
The start and end arguments must be valid indices as for String.substring.
method | ||||
|
The start and end arguments must be valid indices as for String.substring.
method | |
|
method | |
String.copy("apple")
#true
method | |
annotation | |
| |
annotation | |
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
#true
annotation | |
| |
annotation | |
| |
annotation | |
| |
annotation | |