假设我有var lines = IEnumerable<string>,并且lines包含各种行,其中第一个1..n字符将它们从进程中排除.例如以'*','Eg','Sample'等开头的行.
排除令牌列表是可变的,仅在运行时才知道
lines.Where(l => !l.StartsWith("*") && !l.StartsWith("E.g.") && ...
Run Code Online (Sandbox Code Playgroud)
变得有些问题.
我怎么能实现这个目标?
使用LINQ:
List<string> exceptions = new List<string>() { "AA", "EE" };
List<string> lines = new List<string>() { "Hello", "AAHello", "BHello", "EEHello" };
var result = lines.Where(x => !exceptions.Any(e => x.StartsWith(e))).ToList();
// Returns only "Hello", "BHello"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134 次 |
| 最近记录: |