C# 检测换页字符

C# 检测换页字符,c#,file,text,special-characters,streamreader,C#,File,Text,Special Characters,Streamreader,我正在使用C#读取包含表单提要字符的文本文件。 当我遇到以换页字符开头的行时,我需要做一些事情。 我怎样才能检查这个 例如: StreamReader reader = File.OpenText(filePath); while (!reader.EndOfStream) { string currentLine = reader.ReadLine(); //check currentLine to see if it begins with a form feed cha

我正在使用C#读取包含表单提要字符的文本文件。
当我遇到以换页字符开头的行时,我需要做一些事情。 我怎样才能检查这个

例如:

StreamReader reader = File.OpenText(filePath);
while (!reader.EndOfStream)
{
     string currentLine = reader.ReadLine();
     //check currentLine to see if it begins with a form feed character
}

我想你可以这样做:

bool isFormFeed = (currentLine != null) && (currentLine.Length > 0) && (currentLine[0] == '\f');
其中,
“\f”
表示表单提要字符

顺便说一下,最好是这样编写代码:

using (StreamReader reader = File.OpenText(filePath))
{
    // ...
}

i、 e.使用
使用
确保流已关闭。

我认为您可以执行以下操作:

bool isFormFeed = (currentLine != null) && (currentLine.Length > 0) && (currentLine[0] == '\f');
currentLine = currentLine == null ? null : currentLine.TrimStart('\f');
其中,
“\f”
表示表单提要字符

顺便说一下,最好是这样编写代码:

using (StreamReader reader = File.OpenText(filePath))
{
    // ...
}
i、 e.使用
使用
确保流关闭

currentLine = currentLine == null ? null : currentLine.TrimStart('\f');
不能这样做:

string currentLine = reader.ReadLine().TrimStart('\f');
因为您可能会得到一个null ref异常

不能这样做:

string currentLine = reader.ReadLine().TrimStart('\f');
因为您可能会得到一个null ref异常