當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。