假设我有一个类似这样的 txt 文件:
%Title
%colaborations Destination
1 123
2 456
3 555
%my name Destination
Joe doe $re Washington
Marina Panett $re Texas
..
Laura Sonning $mu New York
%other stuff
如何保存到数组中
array{
("Joe doe $re"),
("Marina Panett $re"),
...,
("Laura Sonning $mu")
}
因为我需要跳过:
%Title
%colaborations Destination
1 123
2 456
3 555
直到我找到
%my name Destination
我会开始阅读直到文件结束,或者我找到带有 "%"
我正在考虑使用 string txt = System.IO.File.ReadAllText("file.txt"); 但我不认为读取所有 txt 是个好主意,因为我只需要一些部分...
请您参考如下方法:
您可以使用 Enumerable.SkipWhile 直到找到您要查找的内容:
string[] relevantLines = File.ReadLines(path)
.SkipWhile(l => l != "%my name Destination")
.Skip(1)
.TakeWhile(l =>!l.StartsWith("%"))
.ToArray();
请注意,File.ReadLines 不需要读取整个文件。它类似于 StreamReader。
