我有一个包含此格式的多个条目的文本文件:
one:
name: Bitcoin Reddit
link: https://www.reddit.com/r/Bitcoin/
到目前为止,我已将文本文件读入字符串Page: 1 of 1
Report Date: January 15 2018
Mr. Gerald M. Abridge ID #: 0000008 1 Route 81 Mr. Gerald Michael Abridge Pittaburgh PA 15668 SSN: XXX-XX-XXXX
Birthdate: 01/00/1998 Sex: M
COURSE Course Title CRD GRD GRDPT COURSE Course Title CRD GRD GRDPT
FALL 2017 (08/28/2017 to 12/14/2017) CS102F FUND. OF IT & COMPUTING 4.00 A 16.00 CS110 C++ PROGRAMMING I 3.00 A- 11.10 EL102 LANGUAGE AND RHETORIC 3.00 B+ 9.90 MA109 CALC WITH APPLICATIONS I 4.00 A 16.00 SP203 INTERMEDIATE SPANISH I 3.00 A 12.00
EHRS QHRS QPTS GPA Term 17.00 17.00 65.00 3.824 Cum 17.00 17.00 65.00 3.824
Current Program(s): Bachelor of Science in Computer Science
End of official record.
。我希望能够删除每个条目的前两行。我该怎么做呢?
以下是我用来阅读它的代码:
full
答案 0 :(得分:3)
如果要跳过的所有行都以相同的字符串开头,则可以将这些前缀放在列表中,然后在读取行时跳过任何带有前缀的行:
这将为您提供一个字符串列表,表示所有不以指定前缀之一开头的文件行:
var filePath = @"f:\public\temp\temp.txt";
var ignorePrefixes = new List<string> {"Page:", "Report Date:"};
var filteredContent = File.ReadAllLines(filePath)
.Where(line => ignorePrefixes.All(prefix => !line.StartsWith(prefix)))
.ToList();
如果您希望将所有内容都作为单个字符串,则可以使用String.Join
:
var filteredAsString = string.Join(Environment.NewLine, filteredContent);
如果Linq不是你的事,或者你不明白它在做什么,这就是“老派”做同样事情的方式:
List<string> filtered = new List<string>();
foreach (string line in File.ReadLines(filePath))
{
bool okToAdd = true;
foreach (string prefix in ignorePrefixes)
{
if (line.StartsWith(prefix))
{
okToAdd = false;
break;
}
}
if (okToAdd)
{
filtered.Add(line);
}
}
答案 1 :(得分:0)
public static IEnumerable<string> ReadReportFile(FileInfo file)
{
var line = String.Empty;
var page = "Page:";
var date = "Report Date:";
using(var reader = File.OpenText(file.FullName))
while((line = reader.ReadLine()) != null)
while(line.IndexOf(page) == -1 AND line.IndexOf(date) == -1)
yield return line;
}
代码非常直接,而行不为空,并且不包含页面或日期,返回行。你可以压缩甚至变得更有魅力,建立你的前缀等的查找。但如果代码很简单或不需要那么复杂,这应该就足够了。