The string
module defines the String
type which represents UTF8 encoded strings.
- Struct
String
- Constants
- Function
utf8
- Function
from_ascii
- Function
to_ascii
- Function
try_utf8
- Function
bytes
- Function
into_bytes
- Function
is_empty
- Function
length
- Function
append
- Function
append_utf8
- Function
insert
- Function
sub_string
- Function
index_of
- Function
internal_check_utf8
use 0x1::ascii;
use 0x1::option;
use 0x1::vector;
A String
holds a sequence of bytes which is guaranteed to be in utf8 format.
#[data_struct]
struct String has copy, drop, store
Index out of range.
const EINVALID_INDEX: u64 = 2;
An invalid UTF8 encoding.
const EINVALID_UTF8: u64 = 1;
Creates a new string from a sequence of bytes. Aborts if the bytes do not represent valid utf8.
public fun utf8(bytes: vector<u8>): string::String
Convert an ASCII string to a UTF8 string
public fun from_ascii(s: ascii::String): string::String
Convert an UTF8 string to an ASCII string.
Aborts if s
is not valid ASCII
public fun to_ascii(s: string::String): ascii::String
Tries to create a new string from a sequence of bytes.
public fun try_utf8(bytes: vector<u8>): option::Option<string::String>
Returns a reference to the underlying byte vector.
public fun bytes(s: &string::String): &vector<u8>
Unpack the string
to get its backing bytes
public fun into_bytes(string: string::String): vector<u8>
Checks whether this string is empty.
public fun is_empty(s: &string::String): bool
Returns the length of this string, in bytes.
public fun length(s: &string::String): u64
Appends a string.
public fun append(s: &mut string::String, r: string::String)
Appends bytes which must be in valid utf8 format.
public fun append_utf8(s: &mut string::String, bytes: vector<u8>)
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 string::String, at: u64, o: string::String)
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.
public fun sub_string(s: &string::String, i: u64, j: u64): string::String
Computes the index of the first occurrence of a string. Returns length(s)
if no occurrence found.
public fun index_of(s: &string::String, r: &string::String): u64
public fun internal_check_utf8(v: &vector<u8>): bool