Skip to main content

Module std::string

The string module defines the String type which represents UTF8 encoded strings.

Struct String

A String holds a sequence of bytes which is guaranteed to be in utf8 format.

public struct String has copy, drop, store
Click to open
Fields

Constants

An invalid UTF8 encoding.

const EInvalidUTF8: u64 = 1;

Index out of range.

const EInvalidIndex: u64 = 2;

Function utf8

Creates a new string from a sequence of bytes. Aborts if the bytes do not represent valid utf8.

Function from_ascii

Convert an ASCII string to a UTF8 string

Function to_ascii

Convert an UTF8 string to an ASCII string.
Aborts if s is not valid ASCII

Function try_utf8

Tries to create a new string from a sequence of bytes.

Function as_bytes

Returns a reference to the underlying byte vector.

Function into_bytes

Unpack the string to get its underlying bytes.

Function is_empty

Checks whether this string is empty.

Function length

Returns the length of this string, in bytes.

public fun length(s: &std::string::String): u64

Function append

Appends a string.

Function append_utf8

Appends bytes which must be in valid utf8 format.

Function insert

Insert the other string at the byte index in given string. The index must be at a valid utf8 char boundary.

public fun insert(s: &mut std::string::String, at: u64, o: std::string::String)

Function substring

Returns a sub-string using the given byte indices, where i is the first byte position and j is the start of the first byte not included (or the length of the string). The indices must be at valid utf8 char boundaries, guaranteeing that the result is valid utf8.

Function index_of

Computes the index of the first occurrence of a string. Returns s.length() if no occurrence found.

Function internal_check_utf8

Function internal_is_char_boundary

Function internal_sub_string

Function internal_index_of

Function bytes

public fun bytes(s: &std::string::String): &vector<u8>

Function sub_string