String
- typeString
The string type.
A
Stringliteral is created by putting some text between double quotes.Rotolet 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.
Rotolet 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.
Rotolet 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.
Rotolet a = "haystack".ends_with("stack"); // -> true let b = "haystack".ends_with("black"); // -> false
- functionfrom_chars(chars: List[char]) String
Create a new string from a list of characters.
Rotolet 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
ntimes and join them.Rotolet a = "ha".repeat(6); // -> "hahahahahaha"
- functionreplace(self: String, from: String, to: String) String
Replace all occurrences of
fromwithto.Rotolet 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
separatorat mostntimes starting from the end.If there are more than
n - 1separators, the last list element will contain the remaining prefix of the string.Rotolet a = "Rust!Roto!String".rsplitn(2, "!"); // -> ["String", "Rust!Roto"]
- functionsplit(self: String, separator: String) List[String]
Split a string by a separator.
Rotolet a = "one, two, three".split(", "); // -> ["one", "two", "three"]
- functionsplitn(self: String, n: u64, separator: String) List[String]
Splits this string at
separatorat mostntimes.If there are more than
n - 1separators, the last list element will contain the rest of the string.Rotolet 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.
Rotolet 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
Noneif the string does not contain the prefix.Rotolet a = "RustRoto!".strip_prefix("Rust"); // -> "Roto!"
- functionstrip_suffix(self: String, suffix: String) Option[String]
Create a new string by removing a given suffix.
Returns
Noneif the string does not contain the suffix.Rotolet a = "Roto!Rust".strip_suffix("Rust"); // -> "Roto!"
- functionto_lowercase(self: String) String
Create a new string with all characters converted to lowercase.
Rotolet a = "LOUD".to_lowercase(); // -> "loud"
- functionto_uppercase(self: String) String
Create a new string with all characters converted to uppercase.
Rotolet a = "quiet".to_uppercase(); // -> "QUIET"
- functiontrim(self: String) String
Create a new string by removing leading and trailing whitespace.
Rotolet a = " Roto! ".trim(); // -> "Roto!"