我想要一种可以拆分IEnumerable
谓词的方法,通过它们相对于谓词的索引将项目组合在一起。例如,它可以List<string>
在满足x => MyRegex.Match(x).Success
的项目中拆分 a,将“介于”这些匹配项之间的项目组合在一起。
它的签名看起来有点像
public static IEnumerable<IEnumerable<TSource>> Split<TSource>(
this IEnumerable<TSource> source,
Func<TSource, bool> predicate,
int bool count
)
,可能带有包含所有分隔符的输出的额外元素。
有没有比foreach
循环更有效和/或更紧凑的方法来实现这一点?我觉得应该可以使用 LINQ 方法来实现,但我不能指望它。
例子:
string[] arr = {"One", "Two", "Three", "Nine", "Four", "Seven", "Five"};
arr.Split(x => x.EndsWith("e"));
以下任何一项都可以:
IEnumerable<string> {{}, {"Two"}, {}, {"Four", "Seven"}, {}}
IEnumerable<string> {{"Two"}, {"Four", "Seven"}}
用于存储匹配的可选元素是{"One", "Three", "Nine", "Five"}
.