std/string
String manipulation functions and the String struct.
External Functions
These functions work with the primitive string type.
str_len(s: &const string) -> uint64
Get the length of a string primitive (in bytes).
import "std/string";
let text = "Hello";
let length = str_len(&text); // 5
trim(s: string) -> string
Remove leading and trailing whitespace.
let text = " hello ";
let trimmed = trim(text); // "hello"
to_upper(s: string) -> string
Convert a string to uppercase.
let text = "hello";
let upper = to_upper(text); // "HELLO"
to_lower(s: string) -> string
Convert a string to lowercase.
let text = "HELLO";
let lower = to_lower(text); // "hello"
split(s: &string, sep: &string) -> [string]
Split a string by a separator.
let text = "apple,banana,cherry";
let parts = split(&text, &",");
// ["apple", "banana", "cherry"]
str_cmp(s1: &const string, s2: &const string) -> uint64
Compare two strings lexicographically.
let result = str_cmp(&"apple", &"banana");
to_chars(s: &const string) -> [char]
Convert a string to an array of characters.
let text = "Hi";
let chars = to_chars(&text); // ['H', 'i']
from_chars(s: [char]) -> string
Convert an array of characters to a string.
let chars = ['H', 'i'];
let text = from_chars(chars); // "Hi"
Struct: String
A struct wrapper around the primitive string type with additional methods.
struct String {
public:
s: string;
len: uint64;
}
Constructor
import "std/string";
let str = new String("Hello");
Static Methods
String::from_chars(s: [char]) -> String
Create a String from an array of characters.
let chars = ['H', 'i'];
let str = String::from_chars(chars);
String::str_len(s: &const string) -> uint64
Get the length of a string primitive.
let length = String::str_len(&"Hello"); // 5
Instance Methods
len(&this) -> uint64
Get the length of the String.
let str = new String("Hello");
println(str.len()); // 5
is_empty(&this) -> bool
Check if the String is empty.
let str = new String("");
if str.is_empty() {
println("String is empty");
}
concat(&this, other: String) -> String
Concatenate two Strings.
let str1 = new String("Hello");
let str2 = new String(" World");
let result = str1.concat(str2);
push(&this, c: char)
Append a character to the String.
let str = new String("Hello");
str.push('!');
push_str(&this, s: String)
Append another String.
let str1 = new String("Hello");
let str2 = new String(" World");
str1.push_str(str2);
get(&this, index: uint64) -> char
Get a character at a specific index.
let str = new String("Hello");
let ch = str.get(0u); // 'H'
set(&this, index: uint64, c: char)
Set a character at a specific index.
let str = new String("Hello");
str.set(0u, 'h'); // "hello"
to_str(&this) -> string
Convert the String struct to a string primitive.
let str = new String("Hello");
let s = str.to_str();
to_chars(&this) -> [char]
Convert the String to an array of characters.
let str = new String("Hi");
let chars = str.to_chars();
to_upper(&this) -> String
Convert to uppercase.
let str = new String("hello");
let upper = str.to_upper(); // "HELLO"
to_lower(&this) -> String
Convert to lowercase.
let str = new String("HELLO");
let lower = str.to_lower(); // "hello"
trim(&this) -> String
Remove leading and trailing whitespace.
let str = new String(" hello ");
let trimmed = str.trim(); // "hello"
split(&this, sep: string) -> [String]
Split the String by a separator.
let str = new String("a,b,c");
let parts = str.split(",");
into_iter(this) -> Iter<char>
Consume the String and create an iterator over its characters.
let str = new String("Hi");
let iter = str.into_iter();