在 C# 中使用 Streamreader 跳过行时遇到问题
Having issues using Streamreader to skip lines in C#
我在使用 C#
中的 Streamreader
读取文件时遇到问题,其中我正在读取的文件中的 换行符 也是被读入我的程序并在代码中进一步引起问题。
下面是我尝试读入的文件示例。
Example,1,2,3
<--- // Trying to skip these lines.
<--- // Trying to skip these lines.
// MY COMMENTS.
下面是我目前用来检查换行符的代码。
if (line.StartsWith("//") ||
line.StartsWith("\r\n") ||
line.StartsWith("\n") ||
line.StartsWith("\r") ||
line.StartsWith(" ") ||
line.StartsWith(Environment.NewLine) ||
line.StartsWith(Environment.NewLine) ||
line.StartsWith($" {Environment.NewLine}")) {
// SKIP THE LINE.
break;
}
// ADD TO ARRAY
I have tried checking for line.StartsWith("")
in the if statement
,
but this results in no values being added to my array.
我需要更改哪些内容才能跳过这些行?
https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx
A line is defined as a sequence of characters followed by a line feed
("\n"), a carriage return ("\r"), or a carriage return immediately
followed by a line feed ("\r\n"). The string that is returned does not
contain the terminating carriage return or line feed. The returned
value is null if the end of the input stream is reached.
这篇文章可能会有所帮助:http://www.dotnetperls.com/empty-string
然后尝试 line.IsNullOrEmpty()
(假设您读入的行中没有其他隐藏的空格)。
理论上,每个字符串 'starts with' 什么都没有,所以 "" 将与您读入的每个 line/string 匹配。
if ("Hello, World!".StartsWith(""))
Console.WriteLine("Hello, World!");
return;
打印出语句。
你试过string.IsNullOrWhiteSpace(stringValue)
了吗?你使用'StreamReader.ReadLine()'吗?返回的字符串不包含换行符。
即使你有一个答案(几个),我也会选择一些感觉有点 "cleaner"。
// Read all your lines:
string source = System.IO.File.ReadAllText(path_to_your_file);
// Split them on new lines, ignore any empty lines
var lines = source.Split(Environment.NewLine.ToCharArray()
, StringSplitOptions.RemoveEmptyEntries );
// Iterate over your `lines` here
// ...
我在使用 C#
中的 Streamreader
读取文件时遇到问题,其中我正在读取的文件中的 换行符 也是被读入我的程序并在代码中进一步引起问题。
下面是我尝试读入的文件示例。
Example,1,2,3
<--- // Trying to skip these lines.
<--- // Trying to skip these lines.
// MY COMMENTS.
下面是我目前用来检查换行符的代码。
if (line.StartsWith("//") ||
line.StartsWith("\r\n") ||
line.StartsWith("\n") ||
line.StartsWith("\r") ||
line.StartsWith(" ") ||
line.StartsWith(Environment.NewLine) ||
line.StartsWith(Environment.NewLine) ||
line.StartsWith($" {Environment.NewLine}")) {
// SKIP THE LINE.
break;
}
// ADD TO ARRAY
I have tried checking for
line.StartsWith("")
in theif statement
, but this results in no values being added to my array.
我需要更改哪些内容才能跳过这些行?
https://msdn.microsoft.com/en-us/library/system.io.streamreader.readline(v=vs.110).aspx
A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is null if the end of the input stream is reached.
这篇文章可能会有所帮助:http://www.dotnetperls.com/empty-string
然后尝试 line.IsNullOrEmpty()
(假设您读入的行中没有其他隐藏的空格)。
理论上,每个字符串 'starts with' 什么都没有,所以 "" 将与您读入的每个 line/string 匹配。
if ("Hello, World!".StartsWith(""))
Console.WriteLine("Hello, World!");
return;
打印出语句。
你试过string.IsNullOrWhiteSpace(stringValue)
了吗?你使用'StreamReader.ReadLine()'吗?返回的字符串不包含换行符。
即使你有一个答案(几个),我也会选择一些感觉有点 "cleaner"。
// Read all your lines:
string source = System.IO.File.ReadAllText(path_to_your_file);
// Split them on new lines, ignore any empty lines
var lines = source.Split(Environment.NewLine.ToCharArray()
, StringSplitOptions.RemoveEmptyEntries );
// Iterate over your `lines` here
// ...