pub def str: *char
指向以空字符结尾的字符序列的指针
pub fun str_len(s: str) usize;
计算以空字符结尾的字符串的长度
ret:字符串的长度(不包含空终止符)
pub fun str_empty(s: str) bool;
检查字符串是否为空或 nil
ret:如果字符串长度为零或为 nil,则为 true
pub fun str_equals(s: str, other: str) bool;
比较两个以空字符结尾的字符串是否相等
other:要与之比较的另一个字符串 ret:如果字符串相等则为 true
pub fun str_region_equals(s: str, start: usize, len: usize, other: str) bool;
将字符串的一个区域与模式进行比较
start:在 s 中的字节偏移量 len:要比较的字节数 other:要与之比较的以空字符结尾的字符串 ret:如果 s[start..start+len] 完全等于 other 则为 true
pub fun str_compare(s: str, other: str) i64;
按字典序比较两个以空字符结尾的字符串
other:要与之比较的另一个字符串 ret:若 s < other 返回 <0,相等返回 0,若 s > other 返回 >0
pub fun str_starts_with(s: str, prefix: str) bool;
检查字符串是否以给定的前缀开头
prefix:要检查的前缀 ret:如果字符串以该前缀开头则为 true
pub fun str_ends_with(s: str, suffix: str) bool;
检查字符串是否以给定的后缀结尾
suffix:要检查的后缀 ret:如果字符串以该后缀结尾则为 true
pub fun str_index_of(s: str, sub: str) O.Option[usize];
查找子字符串的第一个索引
sub:要查找的子字符串 ret:首次出现的索引,或 none
pub fun str_index_of_from(s: str, sub: str, from: usize) O.Option[usize];
在起始偏移量处或之后查找子字符串的第一个索引
sub:要查找的子字符串 from:开始扫描的字节偏移量 ret:在 from 处或之后的首次出现的索引,或 none
pub fun str_last_index_of(s: str, sub: str) O.Option[usize];
查找子字符串的最后一个索引
sub:要查找的子字符串 ret:最后一次出现的索引,或 none
pub fun str_contains(s: str, sub: str) bool;
检查字符串是否包含给定的子字符串
sub:要检查的子字符串 ret:如果字符串包含 sub 则为 true
pub fun str_find(s: str, sub: str) O.Option[str];
查找子字符串的第一次出现并返回指向它的指针
sub:要查找的子字符串 ret:指向 s 中首次出现位置的指针,或 none
pub fun str_find_last(s: str, sub: str) O.Option[str];
查找子字符串的最后一次出现并返回指向它的指针
sub:要查找的子字符串 ret:指向 s 中最后一次出现位置的指针,或 none
pub fun str_index_char(s: str, c: char) O.Option[usize];
在字符串中查找字符的第一次出现
c:要查找的字符 ret:首次出现的索引,或 none
pub fun str_last_index_char(s: str, c: char) O.Option[usize];
在字符串中查找字符的最后一次出现
c:要查找的字符 ret:最后一次出现的索引,或 none
pub fun str_contains_char(s: str, c: char) bool;
检查字符串是否包含某个字符
c:要查找的字符 ret:如果 c 出现在 s 中则为 true
pub fun str_find_char(s: str, c: char) O.Option[str];
查找字符的第一次出现并返回指向它的指针
c:要查找的字符 ret:指向 s 中首次出现位置的指针,或 none
pub fun str_find_last_char(s: str, c: char) O.Option[str];
查找字符的最后一次出现并返回指向它的指针
c:要查找的字符 ret:指向 s 中最后一次出现位置的指针,或 none
pub fun str_copy(a: *A.Allocator, s: str) R.Result[str, str];
使用给定的分配器创建字符串的副本
a:用于复制的分配器 s:要复制的字符串 ret:复制后的字符串,或在分配失败时返回错误
pub fun str_copy_slice(a: *A.Allocator, s: str, start: usize, len: usize) R.Result[str, str];
使用给定的分配器创建字符串切片的副本
a:用于复制的分配器 s:要从中复制的字符串 start:切片的起始索引 len:切片的长度 ret:复制后的切片,或在分配失败或索引无效时返回错误
pub fun str_join(a: *A.Allocator, va: ...) R.Result[str, str];
将任意数量的字符串连接为一个新分配的字符串
nil 参数没有贡献,与 str_len/str_empty 一致。零参数调用会返回一个已分配的空字符串。每个参数必须是 str,在编译期强制执行。
a:结果的分配器 va:按顺序要连接的字符串 ret:连接后的字符串,或在分配失败时返回错误
pub fun str_trim(a: *A.Allocator, s: str) R.Result[str, str];
去除字符串开头和结尾的空白字符,返回一个新的分配字符串
a:用于新字符串的分配器 s:要修剪的字符串 ret:修剪后的字符串,或在分配失败时返回错误
pub fun str_trim_right(a: *A.Allocator, s: str) R.Result[str, str];
去除字符串结尾的空白字符,返回一个新的分配字符串
a:用于新字符串的分配器 s:要修剪的字符串 ret:修剪后的字符串,或在分配失败时返回错误
pub fun str_trim_left(a: *A.Allocator, s: str) R.Result[str, str];
去除字符串开头的空白字符,返回一个新的分配字符串
a:用于新字符串的分配器 s:要修剪的字符串 ret:修剪后的字符串,或在分配失败时返回错误
pub fun str_to_lower(a: *A.Allocator, s: str) R.Result[str, str];
将字符串转换为小写(ASCII),返回一个新的分配字符串
a:结果的分配器 s:要转换为小写的字符串 ret:小写化的字符串,或在分配失败时返回错误
pub fun str_to_upper(a: *A.Allocator, s: str) R.Result[str, str];
将字符串转换为大写(ASCII),返回一个新的分配字符串
a:结果的分配器 s:要转换为大写的字符串 ret:大写化的字符串,或在分配失败时返回错误
pub fun str_repeat(a: *A.Allocator, s: str, n: usize) R.Result[str, str];
将字符串重复 n 次,返回一个新的分配字符串
a:结果的分配器 s:要重复的字符串 n:重复次数(0 会产生一个空字符串) ret:重复后的字符串,或在分配失败或大小溢出时返回错误
pub fun str_replace(a: *A.Allocator, s: str, old: str, new: str) R.Result[str, str];
替换子字符串的所有出现项,返回一个新的分配字符串
从左到右查找出现项且不重叠。old 为 nil 或空字符串会产生 s 的普通副本。
a:结果的分配器 s:要在其中进行替换的字符串 old:要替换的子字符串 new:替换后的子字符串 ret:替换后的字符串,或在分配失败时返回错误