String
A String represents text, a sequence of characters.
let message be "hello world"A multiline string can be written with quote, followed by a sequence of lines
starting with |.
let long_message be quote
| this is a
| multiline string
| with lots of words
| to be readStrings can be interpolated with other values using {} delimiters. The
expression inside the {} will be automatically converted using to_string.
let name be "Alice"
let age be 30
print "I'm {name} and I'm {age} years old."Strings support escape characters.
let message be "some text in \"quotes\", ending in a new line\n"\u####: Four hex digits to produce a Unicode codepoint (0-65535).\x##: Two hex digits to produce a byte (0-255).\n: A new line.\r: A carriage return.\0: A null character.\t: A tab.\#: To escape any single character#that isn't any of the above, including the backslash itself\\.
Usage
Individual characters can be accessed using at, but not updated. This
creates a new string with the character at the given index.
let message be "hello world"
print message at 0 # "h"Strings can be concatenated with other strings using the plus operator.
let hello be "hello" plus " " plus "world"
print hello # "hello world"Constructor
Creates a new String by converting the given value via to_string.
let a be new String with just 42Getters
Returns the number of characters in the string.
let message be "hello world"
print message's length # 11A getter that returns an Iterator to iterate over the string's individual
characters.
Methods
Returns a copy of itself.
Returns a string representation of the string in JSON format.
Returns true if the string contains the given substring.
Returns a lowercase version of the string.
- e.g.
Hello Worldbecomeshello world.
Returns a uppercase version of the string.
- e.g.
hello worldbecomesHELLO WORLD.
Returns the same string but with the first character capitalized.
- e.g.
hello worldbecomesHello world.
Returns the same string but with the first character of each word capitalized.
- e.g.
hello worldbecomesHello World.
Returns a list of strings split by the delimiter, converted with to_string.
let message be "hello world"
let split_message be message's to_split " "
print split_message # "list with hello, and world"Returns a slice of the string. If end is not specified, it defaults to the end of
the string.
let message be "hello world"
let sliced_message be message's to_slice with 0, and 5
print sliced_message # "hello"Returns the same string but with leading and trailing whitespaces removed.
let message be " hello world "
let trimmed_message be do message's to_trim
print trimmed_message # "hello world"print "hello world"