index_of
pub fn index_of(s &string, sub string) int用途
返回 sub 在 s 中首次出现的字节下标。空 sub 返回 0;未找到返回 -1。查找走 Horspool。
参数
s&string- 被查找的字符串(借用,调用方仍持有)
substring- 要查找的子串
返回
首次出现的下标;未找到为 -1;空 sub 为 0
API
#strings字符串工具。只读函数吃 &string,调用方仍持有源。查找走 Horspool。含 Builder / Reader / Replacer。
index_ofpub fn index_of(s &string, sub string) int返回 sub 在 s 中首次出现的字节下标。空 sub 返回 0;未找到返回 -1。查找走 Horspool。
s &stringsub string首次出现的下标;未找到为 -1;空 sub 为 0
containspub fn contains(s &string, sub string) bool判断 s 是否包含子串 sub。空 sub 视为包含(下标 0)。
s &stringsub string包含则为 true,否则 false
trim_spacepub fn trim_space(s &string) string去掉两端 ASCII 空白,返回新串。
s &stringstring — 结果字符串
repeatpub fn repeat(s &string, n int) string把内容重复 n 次;n<=0 得空串。
s &stringn intstring — 结果字符串
joinpub fn join(parts []string, sep string) string用 sep 把 parts 拼成一个字符串。空切片得到空串。
parts []stringsep string拼接后的 string
splitpub fn split(s &string, sep string) []string按 sep 切开 s。空 sep 不按字符拆,而是返回仅含整个 s 的单元素切片。
s &stringsep string切开后的 []string(含末尾空段)
has_prefixpub fn has_prefix(s &string, pre string) bool判断 s 是否以 pre 开头。
s &stringpre string是则 true
has_suffixpub fn has_suffix(s &string, suf string) bool判断 s 是否以 suf 结尾。
s &stringsuf string是则 true
ends_withpub fn ends_with(s &string, suf string) bool判断 s 是否以 suf 结尾(与 has_suffix 同类)。
s &stringsuf string是则 true
replacepub fn replace(s &string, old string, new string, n int) string把 s 里的 old 换成 new。n==0 或 old 为空则原样返回;n<0 替换全部;n>0 最多替换 n 次。
s &stringold stringnew stringn int替换后的新 string
fieldspub fn fields(s &string) []string按空白段切开,丢掉空白本身。
s &string[]string — 切片结果
fields_funcpub fn fields_func(s &string, f fn(int) bool) []string按回调为真的字节切开。
s &stringf fn(int) bool[]string — 切片结果
to_lowerpub fn to_lower(s &string) stringASCII 字母转小写。
s &stringstring — 结果字符串
to_upperpub fn to_upper(s &string) stringASCII 字母转大写。
s &stringstring — 结果字符串
trim_leftpub fn trim_left(s &string, cutset string) string去掉左端出现在 cutset 中的字节。
s &stringcutset stringstring — 结果字符串
trim_rightpub fn trim_right(s &string, cutset string) string去掉右端出现在 cutset 中的字节。
s &stringcutset stringstring — 结果字符串
trimpub fn trim(s &string, cutset string) string去掉两端出现在 cutset 中的字节。
s &stringcutset stringstring — 结果字符串
trim_prefixpub fn trim_prefix(s &string, pre string) string若有前缀则去掉该前缀,否则原样返回。
s &stringpre stringstring — 结果字符串
trim_suffixpub fn trim_suffix(s &string, suf string) string若有后缀则去掉该后缀,否则原样返回。
s &stringsuf stringstring — 结果字符串
comparepub fn compare(a &string, b &string) int字典序比较:小于 -1、相等 0、大于 1。
a &stringb &stringint — 小于 -1、相等 0、大于 1
index_bytepub fn index_byte(s &string, c int) int返回字节 c 首次出现的下标;未找到为 -1。
s &stringc intint — 下标;未找到为 -1
last_indexpub fn last_index(s &string, sub string) int返回最后一次出现的下标;未找到为 -1。未找到返回 -1。
s &stringsub stringint — 下标;未找到为 -1
last_index_bytepub fn last_index_byte(s &string, c int) int返回字节 c 最后一次出现的下标;未找到为 -1。
s &stringc intint — 下标;未找到为 -1
countpub fn count(s &string, sub string) int统计 sub 在 s 中非重叠出现次数。空 sub 返回 1+s.len。
s &stringsub string出现次数(int)
split_npub fn split_n(s &string, sep string, n int) []string按分隔符切开,最多 n 段。
s &stringsep stringn int[]string — 切片结果
replace_allpub fn replace_all(s &string, old string, new string) string替换全部出现的子串。
s &stringold stringnew stringstring — 结果字符串
equal_foldpub fn equal_fold(a &string, b &string) boolASCII 大小写不敏感比较。
a &stringb &stringbool — 是否成立
struct Cutpub struct Cut {
pub before string
pub after string
pub found bool
}公开结构体 `Cut`。
before stringafter stringfound boolcutpub fn cut(s &string, sep string) Cut在首个 sep 处切成前后两段。
s &stringsep stringCut — 该类型的值
cut_prefixpub fn cut_prefix(s &string, pre string) Cut若有前缀则切掉。
s &stringpre stringCut — 该类型的值
cut_suffixpub fn cut_suffix(s &string, suf string) Cut若有后缀则切掉。
s &stringsuf stringCut — 该类型的值
struct Builderpub struct Builder {
pub buf string
}公开结构体 `Builder`。
buf stringbuilderpub fn builder() Builder构建器。
无
Builder — 该类型的值
(&mut Builder) write_stringpub fn (b &mut Builder) write_string(s &string) int`&mut Builder` 上的方法:写入字符串,返回写入的字节数。
b &mut Builders &stringint — 写入的字节数
(&mut Builder) growpub fn (b &mut Builder) grow(n int)`&mut Builder` 上的方法:保证至少还能再写入 n 字节。
b &mut Buildern int(&mut Builder) write_viewpub fn (b &mut Builder) write_view(s &string, v ByteView) int`&mut Builder` 上的方法:写入view。
b &mut Builders &stringv ByteViewint — 该类型的值
(&mut Builder) write_intpub fn (b &mut Builder) write_int(n int) int`&mut Builder` 上的方法:写入int。
b &mut Buildern intint — 该类型的值
(&Builder) lengthpub fn (b &Builder) length() int`&Builder` 上的方法:返回当前长度。
b &Builderint — 该类型的值
(&Builder) textpub fn (b &Builder) text() string`&Builder` 上的方法:返回当前文本。
b &Builderstring — 结果字符串
(&mut Builder) resetpub fn (b &mut Builder) reset()`&mut Builder` 上的方法:清空内部状态,可再写入或读取。
b &mut Builderindex_funcpub fn index_func(s &string, f fn(int) bool) int返回使回调为真的首个字节下标;未找到为 -1。未找到返回 -1。
s &stringf fn(int) boolint — 下标;未找到为 -1
last_index_funcpub fn last_index_func(s &string, f fn(int) bool) int返回使回调为真的最后字节下标;未找到为 -1。未找到返回 -1。
s &stringf fn(int) boolint — 下标;未找到为 -1
map_bytespub fn map_bytes(s &string, f fn(int) int) string对每个字节调用 f,用返回值重建字符串。
s &stringf fn(int) intstring — 结果字符串
struct ByteBuilderpub struct ByteBuilder {
pub buf []int
pub len int
}公开结构体 `ByteBuilder`。
buf []intlen intnew_byte_builderpub fn new_byte_builder(cap int) ByteBuilder构造 byte builder。
cap intByteBuilder — 该类型的值
(&mut ByteBuilder) reservepub fn (sb &mut ByteBuilder) reserve(extra int)`&mut ByteBuilder` 上的方法:预留。
sb &mut ByteBuilderextra int(&mut ByteBuilder) write_bytepub fn (sb &mut ByteBuilder) write_byte(c int)`&mut ByteBuilder` 上的方法:写入一个字节。
sb &mut ByteBuilderc int(&mut ByteBuilder) write_strpub fn (sb &mut ByteBuilder) write_str(s string)`&mut ByteBuilder` 上的方法:写入字符串。
sb &mut ByteBuilders string(ByteBuilder) to_stringpub fn (sb ByteBuilder) to_string() string`ByteBuilder` 上的方法:转成 string。
sb ByteBuilderstring — 结果字符串
clonepub fn clone(s &string) string拷贝一份独立的 string。
s &stringstring — 结果字符串
contains_funcpub fn contains_func(s &string, f fn(int) bool) bool判断是否存在使回调为真的字节。
s &stringf fn(int) boolbool — 是否成立
index_anypub fn index_any(s &string, chars string) int返回 cutset 中任一字节首次出现的下标;未找到为 -1。未找到返回 -1。
s &stringchars stringint — 下标;未找到为 -1
last_index_anypub fn last_index_any(s &string, chars string) int返回 cutset 中字节最后一次出现的下标;未找到为 -1。未找到返回 -1。
s &stringchars stringint — 下标;未找到为 -1
contains_anypub fn contains_any(s &string, chars string) bool判断是否包含 cutset 中任一字节。
s &stringchars stringbool — 是否成立
index_runepub fn index_rune(s &string, r int) int返回 rune 首次出现的下标;未找到为 -1。未找到返回 -1。
s &stringr intint — 下标;未找到为 -1
contains_runepub fn contains_rune(s &string, r int) bool判断是否包含给定 rune。
s &stringr intbool — 是否成立
split_afterpub fn split_after(s &string, sep string) []string按分隔符切开,分隔符留在各段末尾。
s &stringsep string[]string — 切片结果
split_after_npub fn split_after_n(s &string, sep string, n int) []string按分隔符切开并保留分隔符,最多 n 段。
s &stringsep stringn int[]string — 切片结果
trim_left_funcpub fn trim_left_func(s &string, f fn(int) bool) string按回调去掉左端字节。
s &stringf fn(int) boolstring — 结果字符串
trim_right_funcpub fn trim_right_func(s &string, f fn(int) bool) string按回调去掉右端字节。
s &stringf fn(int) boolstring — 结果字符串
trim_funcpub fn trim_func(s &string, f fn(int) bool) string按回调去掉两端字节。
s &stringf fn(int) boolstring — 结果字符串
to_valid_utf8pub fn to_valid_utf8(s &string, replacement string) string把非法 UTF-8 序列换成 replacement。
s &stringreplacement stringstring — 结果字符串
(&mut Builder) write_bytepub fn (b &mut Builder) write_byte(c int) int`&mut Builder` 上的方法:写入一个字节。
b &mut Builderc intint — 写入的字节数
(&mut Builder) write_runepub fn (b &mut Builder) write_rune(r int) int`&mut Builder` 上的方法:写入一个 rune 的 UTF-8 编码。
b &mut Builderr intint — 写入的字节数
(&Builder) cappub fn (b &Builder) cap() int`&Builder` 上的方法:返回容量。
b &Builderint — 该类型的值
struct Replacerpub struct Replacer {
}公开结构体 `Replacer`。
无
new_replacerpub fn new_replacer(oldnew <-> string) Replacer构造 replacer。失败时 throw。
oldnew <-> stringReplacer — 该类型的值
(&Replacer) replacepub fn (r &Replacer) replace(s0 &string) string把 s 里的 old 换成 new。n==0 或 old 为空则原样返回;n<0 替换全部;n>0 最多替换 n 次。
r &Replacers0 &string替换后的新 string
struct Readerpub struct Reader {
}公开结构体 `Reader`。
无
struct RuneReadpub struct RuneRead {
pub ch int
pub size int
}公开结构体 `RuneRead`。
ch intsize intnew_readerpub fn new_reader(s string) Reader构造 reader。
s stringReader — 该类型的值
(&Reader) sizepub fn (r &Reader) size() int`&Reader` 上的方法:返回尺寸。
r &Readerint — 该类型的值
(&Reader) lengthpub fn (r &Reader) length() int`&Reader` 上的方法:返回当前长度。
r &Readerint — 该类型的值
(&mut Reader) resetpub fn (r &mut Reader) reset(s string)`&mut Reader` 上的方法:清空内部状态,可再写入或读取。
r &mut Readers string(&mut Reader) read_npub fn (r &mut Reader) read_n(n int) string`&mut Reader` 上的方法:最多读 n 字节;到末尾可返回更短或空串。
r &mut Readern intstring — 结果字符串
(&mut Reader) read_bytepub fn (r &mut Reader) read_byte() int`&mut Reader` 上的方法:读一个字节;失败时 throw。
r &mut Readerint — 该类型的值
(&mut Reader) unread_bytepub fn (r &mut Reader) unread_byte() int`&mut Reader` 上的方法:把最近读出的一字节退回。失败时 throw。
r &mut Readerint — 该类型的值
(&mut Reader) read_runepub fn (r &mut Reader) read_rune() RuneRead`&mut Reader` 上的方法:读一个 UTF-8 rune。失败时 throw。
r &mut ReaderRuneRead — 该类型的值
(&mut Reader) unread_runepub fn (r &mut Reader) unread_rune() int`&mut Reader` 上的方法:把最近读出的 rune 退回。失败时 throw。
r &mut Readerint — 该类型的值
(&mut Reader) seekpub fn (r &mut Reader) seek(offset int, whence int) int`&mut Reader` 上的方法:按 whence 把读位置移到 offset。whence 0/1/2 分别为绝对、相对、从末尾。失败时 throw。
r &mut Readeroffset intwhence intint — 该类型的值
(&mut Reader) read_atpub fn (r &mut Reader) read_at(n int, off int) string`&mut Reader` 上的方法:从偏移 off 起最多读 n 字节。失败时 throw。
r &mut Readern intoff intstring — 结果字符串
struct ByteViewpub struct ByteView {
pub off int
pub len int
}公开结构体 `ByteView`。
off intlen intview_ofpub fn view_of(s &string) ByteViewviewof。
s &stringByteView — 该类型的值
view_slicepub fn view_slice(s &string, off int, n int) ByteViewview切片。
s &stringoff intn intByteView — 该类型的值
(&ByteView) atpub fn (v &ByteView) at(s &string, i int) int在 `&ByteView` 上调用 `at`。
v &ByteViews &stringi intint — 该类型的值
(&ByteView) equalpub fn (v &ByteView) equal(s &string, t &string, w ByteView) bool`&ByteView` 上的方法:判断两值是否相等。
v &ByteViews &stringt &stringw ByteViewbool — 是否成立
(&ByteView) comparepub fn (v &ByteView) compare(s &string, t &string, w ByteView) int`&ByteView` 上的方法:字典序比较:小于 -1、相等 0、大于 1。
v &ByteViews &stringt &stringw ByteViewint — 小于 -1、相等 0、大于 1
(&ByteView) index_bytepub fn (v &ByteView) index_byte(s &string, c int) int`&ByteView` 上的方法:返回字节 c 首次出现的下标;未找到为 -1。未找到返回 -1。
v &ByteViews &stringc intint — 下标;未找到为 -1
(&ByteView) last_index_bytepub fn (v &ByteView) last_index_byte(s &string, c int) int`&ByteView` 上的方法:返回字节 c 最后一次出现的下标;未找到为 -1。未找到返回 -1。
v &ByteViews &stringc intint — 下标;未找到为 -1
(&ByteView) has_prefixpub fn (v &ByteView) has_prefix(s &string, pre &string) bool判断 s 是否以 pre 开头。
v &ByteViews &stringpre &string是则 true
(&ByteView) has_suffixpub fn (v &ByteView) has_suffix(s &string, suf &string) bool判断 s 是否以 suf 结尾。
v &ByteViews &stringsuf &string是则 true
(&ByteView) indexpub fn (v &ByteView) index(s &string, sub &string) int`&ByteView` 上的方法:返回首次出现的下标;未找到为 -1。未找到返回 -1。
v &ByteViews &stringsub &stringint — 下标;未找到为 -1