当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust str.split_inclusive用法及代码示例


本文简要介绍rust语言中 str.split_inclusive 的用法。

用法

pub fn split_inclusive<'a, P>(&'a self, pat: P) -> SplitInclusive<'a, P> where    P: Pattern<'a>,

此字符串切片的子字符串的迭代器,由模式匹配的字符分隔。与split 生成的迭代器不同,split_inclusive 将匹配的部分作为子字符串的终止符。

pattern 可以是 &str char char 的切片,或者确定字符是否匹配的函数或闭包。

例子

let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb."
    .split_inclusive('\n').collect();
assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb."]);

如果字符串的最后一个元素匹配,则该元素将被视为前一个子字符串的终止符。该子字符串将是迭代器返回的最后一项。

let v: Vec<&str> = "Mary had a little lamb\nlittle lamb\nlittle lamb.\n"
    .split_inclusive('\n').collect();
assert_eq!(v, ["Mary had a little lamb\n", "little lamb\n", "little lamb.\n"]);

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 str.split_inclusive。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。