String

typeString

The string type.

A String literal is created by putting some text between double quotes.

Roto
let a = "Hello!";

See the language reference for more information. Roto supports string formatting when a string literal is prefixed with an f.

functionappend(self: String, other: String) String

Append a string to another, creating a new string.

Roto
let a = "hello".append(" ").append("world"); // -> "hello world"
functionbytes(self: String) StringBytes

Get a view of this string indexed by bytes.

functionchars(self: String) StringChars

Get a view of this string indexed by chars.

functioncontains(self: String, needle: String) bool

Check whether a string contains another string.

Roto
let a = "haystack".contains("hay");  // -> true
let b = "haystack".contains("corn"); // -> false
functionends_with(self: String, suffix: String) bool

Check whether a string ends with a given suffix.

Roto
let a = "haystack".ends_with("stack"); // -> true
let b = "haystack".ends_with("black"); // -> false
functioneq(self: String, other: String) bool

Check for string equality.

functionfrom_chars(chars: List[char]) String

Create a new string from a list of characters.

Roto
let a = String.from_chars(['h', 'e', 'l', 'l', 'o']); // -> "hello"
functionlines(self: String) StringLines

Get a view of this string indexed by lines.

functionrepeat(self: String, n: u64) String

Repeat a string n times and join them.

Roto
let a = "ha".repeat(6); // -> "hahahahahaha"
functionreplace(self: String, from: String, to: String) String

Replace all occurrences of from with to.

Roto
let a = "In rust we trust".replace("rust", "roto"); // -> "In roto we troto"
functionrsplitn(self: String, n: u64, separator: String) List[String]

Splits this string at separator at most n times starting from the end.

If there are more than n - 1 separators, the last list element will contain the remaining prefix of the string.

Roto
let a = "Rust!Roto!String".rsplitn(2, "!");
// -> ["String", "Rust!Roto"]
functionsplit(self: String, separator: String) List[String]

Split a string by a separator.

Roto
let a = "one, two, three".split(", "); // -> ["one", "two", "three"]
functionsplitn(self: String, n: u64, separator: String) List[String]

Splits this string at separator at most n times.

If there are more than n - 1 separators, the last list element will contain the rest of the string.

Roto
let a = "Rust!Roto!String".splitn(2, "!");
// -> ["Rust", "Roto!String"]
functionstarts_with(self: String, prefix: String) bool

Check whether a string starts with a given prefix.

Roto
let a = "haystack".starts_with("hay");   // -> true
let b = "haystack".starts_with("trees"); // -> false
functionstrip_prefix(self: String, prefix: String) Option[String]

Create a new string by removing a given prefix.

Returns None if the string does not contain the prefix.

Roto
let a = "RustRoto!".strip_prefix("Rust"); // -> "Roto!"
functionstrip_suffix(self: String, suffix: String) Option[String]

Create a new string by removing a given suffix.

Returns None if the string does not contain the suffix.

Roto
let a = "Roto!Rust".strip_suffix("Rust"); // -> "Roto!"
functionto_lowercase(self: String) String

Create a new string with all characters converted to lowercase.

Roto
let a = "LOUD".to_lowercase(); // -> "loud"
functionto_string(self: String) String

Convert this value into a String

functionto_uppercase(self: String) String

Create a new string with all characters converted to uppercase.

Roto
let a = "quiet".to_uppercase(); // -> "QUIET"
functiontrim(self: String) String

Create a new string by removing leading and trailing whitespace.

Roto
let a = "  Roto!  ".trim(); // -> "Roto!"
functiontrim_end(self: String) String

Create a new string by removing trailing whitespace.

Roto
let a = "  Roto!  ".trim_end(); // -> "  Roto!"
functiontrim_start(self: String) String

Create a new string by removing leading whitespace.

Roto
let a = "  Roto!  ".trim_start(); // -> "Roto!  "