确定文本文件中使用的行结尾
C# 中确定文本文件(Unix、Windows、Mac)中使用的行结尾的最佳方法是什么?
Whats the best way in C# to determine the line endings used in a text file (Unix, Windows, Mac)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请注意,文本文件可能具有不一致的行结尾。你的程序不应该因此而卡住。在
StreamReader
(和类似方法)上使用ReadLine
将自动处理任何可能的行结束。如果您手动从文件中读取行,请确保接受任何行结尾,即使不一致。实际上,使用以下算法非常容易:
Notice that text files may have inconsistent line endings. Your program should not choke on that. Using
ReadLine
on aStreamReader
(and similar methods) will take care of any possible line ending automatically.If you manually read lines from a file, make sure to accept any line endings, even if inconsistent. In practice, this is quite easy using the following algorithm:
这里有一些高级猜测:读取文件,计算 CR 和 LF
另请注意,较新的 Mac (Mac OS X) 使用 Unix 行结尾
Here is some advanced guesswork: read the file, count CRs and LFs
Also note, that newer Macs (Mac OS X) use Unix line endings
我只需在文件中搜索第一个
\r
或\n
,如果它是\n
我会查看上一个字符来查看它是否是\r
,如果是,则为\r\n
,否则以找到的为准。I'd just search the file for the first
\r
or\n
and if it was a\n
I'd look at the previous character to see if it's a\r
, if so, it's\r\n
otherwise it's whichever found.我想你不能确定,必须在编辑器中设置它。你可以使用一些人工智能,算法是:
因此,例如,如果您在 38、40、45 处重复了 CRLF,并且这在容差范围内,那么您将默认假设行结尾是 CRLF。
I would imagine you couldn't know for sure, would have to set this in the editor. You could use some AI, the algorithm would be:
So, for example, if you had repeats of CRLF at 38, 40, 45, and that was within tolerance you'd default to assuming the line end was CRLF.
如果是我,我只会一次读取一个字符,直到遇到第一个
\r
或\n
。这是假设你有理智的输入。If it were me, I'd just read the file one char at a time until I came across the first
\r
or a\n
. This is assuming you have sensical input.阅读大多数文本格式时,我通常会查找 \n,然后 Trim() 整个字符串(开头和结尾的空格通常是多余的)。
Reading most of textual formats I usually look for \n, and then Trim() the whole string (whitespaces at beginning and end are often redundant).
虽然有
Environment.NewLine
,但它仅用于确定当前系统上使用的内容,并且无助于从各种来源读取文件。如果它正在读取,我通常会查找
\n
(编辑:显然有一些只使用\r
)并假设该行在那里结束。There is
Environment.NewLine
though that is only for determining what is used on the current system and won't help with reading files from various sources.If it's reading I usually look for
\n
(Edit: apperantly there are some using only\r
) and assume that the line ends there.