pub fn SplitN(
s: impl AsRef<[byte]>,
sep: impl AsRef<[byte]>,
n: int,
) -> Vec<Vec<byte>>Expand description
SplitN slices s into subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, SplitN splits after each UTF-8 sequence.
The count determines the number of subslices to return:
n > 0: at most n subslices; the last subslices will be the unsplit remainder.
n == 0: the result is [] (zero subslices)
n < 0: all subsliceszh-cn
用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有片段组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个字符串。参数n决定返回的切片的数目:n > 0 : 返回的切片最多n个子字符串;最后一个子字符串包含未进行切割的部分。
n == 0: 返回[]
n < 0 : 返回所有的子字符串组成的切片§Example
use gostd_bytes as bytes;
use gostd_builtin::byte;
assert_eq!(Vec::<Vec<byte>>::new(), bytes::SplitN(",a,b2,c".as_bytes(), ",", 0));
assert_eq!(vec![",a,b2,c".as_bytes().to_vec()], bytes::SplitN(",a,b2,c".as_bytes(), ",", 1));
assert_eq!(
vec!["", "a", "b2", "c"].iter().map(|x|x.as_bytes().to_vec()).collect::<Vec<Vec<byte>>>(), bytes::SplitN(",a,b2,c".as_bytes(), ",", -1)
);
assert_eq!(vec!["", "a,b2,c"].iter().map(|x|x.as_bytes().to_vec()).collect::<Vec<Vec<byte>>>(), bytes::SplitN(",a,b2,c".as_bytes(), ",", 2));
assert_eq!(vec!["", "a", "b2,c"].iter().map(|x|x.as_bytes().to_vec()).collect::<Vec<Vec<byte>>>(), bytes::SplitN(",a,b2,c".as_bytes(), ",", 3));
assert_eq!(
vec!["", "a", "b2", "c"].iter().map(|x|x.as_bytes().to_vec()).collect::<Vec<Vec<byte>>>(),
bytes::SplitN(",a,b2,c".as_bytes(), ",", 4)
);
assert_eq!(
vec!["", "a", "b2", "c"].iter().map(|x|x.as_bytes().to_vec()).collect::<Vec<Vec<byte>>>(),
bytes::SplitN(",a,b2,c".as_bytes(), ",", 5)
);
assert_eq!(
vec!["", "a", "b2", "c"].iter().map(|x|x.as_bytes().to_vec()).collect::<Vec<Vec<byte>>>(),
bytes::SplitN(",a,b2,c".as_bytes(), ",", 10)
);