string

string: string, empty, size, first, last, operator[], operator[-], chars, data, contains, indexOf, startsWith, endsWith, substr, toUpper, toLower, hash, iterator, lines, bytes, parseInt, parseFloat, parseFloat64, escape, repeat, print, compare, all, any, none

Functions: operator==

struct string: 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.

first

char first()

Returns the first character.

last

char last()

Returns the last 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; out-of-bounds access aborts in debug and release-safe builds.

chars

char[] chars()

Returns the characters as a slice.

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.

indexOf

int? indexOf(char c)

Returns the index of the given character, or null if it's not found.

int? indexOf(char c, int start)

Returns the index of the given character, or null if it's not found. Starts from start.

int? indexOf(string needle)

Returns the index of the given substring, or null if it's not found.

int? indexOf(string needle, int start)

Returns the index of the given substring, or null 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]

toUpper

StringBuf toUpper()

Returns a copy of the string with all characters converted to uppercase (ASCII only).

toLower

StringBuf toLower()

Returns a copy of the string with all characters converted to lowercase (ASCII only).

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.

parseFloat

float? parseFloat()

Parses the string as a 32-bit float, or returns null. Accepts an optional sign, digits with an optional fraction and 'e'/'E' exponent, and case-insensitive "inf"/"infinity"/"nan". The whole string must be a number; values that overflow return null (values that underflow return zero).

parseFloat64

float64? parseFloat64()

Parses the string as a 64-bit float, or returns null. See parseFloat.

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.

print

void print(OutputStream& stream)

Writes this string to the given stream.

compare

Ordering compare(string& other)

Compares this string to another.

all

bool all<Pred: bool(char&)>(Pred predicate)

Returns true if all characters satisfy the predicate.

bool all<T: Copyable, Pred: bool(T)>(Pred predicate)

Returns true if all characters satisfy the predicate.

any

bool any<Pred: bool(char&)>(Pred predicate)

Returns true if any character satisfies the predicate.

bool any<T: Copyable, Pred: bool(T)>(Pred predicate)

Returns true if any character satisfies the predicate.

none

bool none<Pred: bool(char&)>(Pred predicate)

Returns true if no character satisfies the predicate.

bool none<T: Copyable, Pred: bool(T)>(Pred 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.