string
string: string, empty, size, front, operator[], operator[-], data, contains, find, indexOf, startsWith, endsWith, substr, hash, iterator, lines, bytes, parseInt, escape, repeat, print, compare, all, any, none
Functions: operator==
struct string: Copyable, Comparable, Hashable, Printable
A non-owning string view.
string
string(char* pointer, int length)Initializes a string of the given length at the given address.
string(char* cString)Initializes a string referring to a null-terminated C-style string.
Note: This constructor has a time complexity of O(n)
because it has to calculate the length of the argument string. If you
know the length beforehand, consider using
string(char* pointer, int length) instead.
implicit string(StringBuf& stringBuf)Initializes a string viewing the given buffer.
empty
bool empty()Returns true if the string has no characters.
size
int size()Returns the number of characters.
front
char front()Returns the first character.
operator[]
char operator[](int index)Returns the character at the given index.
operator[-]
char operator[-](int index)Returns the character at the given offset from the end. Offset 1 is the last character; offsets outside 1..size() abort.
data
char[*] data()Returns a pointer to the first character.
contains
bool contains(char c)Returns true if the string contains the given character.
bool contains(string needle)Returns true if the string contains the given substring.
find
int find(char c)Returns the index of the given character, or the size if it's not found.
int find(string needle)Returns the index of the given substring, or the size if it's not found.
int find(string needle, int start)Returns the index of the given substring, or the size if it's not
found. Starts from start.
indexOf
int indexOf(char c)Returns the index of the given character, or the size if it's not found.
int indexOf(char c, int start)Returns the index of the given character, or the size if it's not
found. Starts from start.
int indexOf(string needle)Returns the index of the given substring, or the size if it's not found.
int indexOf(string needle, int start)Returns the index of the given substring, or the size if it's not
found. Starts from start.
startsWith
bool startsWith(string prefix)Returns true if the string starts with the given prefix.
endsWith
bool endsWith(string suffix)Returns true if the string ends with the given suffix.
substr
string substr(int start)Returns the substring of the string starting from the given index, until the end of the string.
string substr(Range<int> range)Returns the substring of the string in the given range, [inclusive, exclusive]
hash
uint64 hash()Supports using strings with sets and dicts
iterator
StringIterator iterator()Returns an iterator over the characters.
lines
LineIterator lines()Returns an iterator over each line in the string.
bytes
ByteIterator bytes()Returns an iterator over each byte in the string.
parseInt
int? parseInt()Parses the string as an integer, or returns null. Supports an optional leading '+' or '-' sign and '0x', '0b', and '0o' base prefixes. Out-of-range values return null.
escape
StringBuf escape()Returns the string with special characters escaped.
repeat
RepeatIterator<string> repeat(int count)Returns the string repeated the given number of times, without allocating.
void print(OutputStream& stream)Writes this string to the given stream.
compare
Ordering compare(string& other)Compares this string to another.
all
bool all(bool(char&) predicate)Returns true if all characters satisfy the predicate.
bool all<T: Copyable>(bool(T) predicate)Returns true if all characters satisfy the predicate.
any
bool any(bool(char&) predicate)Returns true if any character satisfies the predicate.
bool any<T: Copyable>(bool(T) predicate)Returns true if any character satisfies the predicate.
none
bool none(bool(char&) predicate)Returns true if no character satisfies the predicate.
bool none<T: Copyable>(bool(T) predicate)Returns true if no character satisfies the predicate.
operator==
bool operator==(string a, string b)Returns true if both strings are equal.
bool operator==(char* a, string b)Returns true if both strings are equal.
bool operator==(string a, char* b)Returns true if both strings are equal.
bool operator==(RepeatIterator<string> a, string b)Returns true if the repetition equals the string, without allocating.
bool operator==(string a, RepeatIterator<string> b)Returns true if the repetition equals the string, without allocating.